C
C
Clif19862021-02-09 12:30:13
Google Sheets
Clif1986, 2021-02-09 12:30:13

How to copy a page to the left, to the right of a template with a script in Google sheets?

When the button is pressed, the page is copied using the script:

sheet2 = sheet.copyTo(ss).setName(sheet2.getResponseText());


But this sheet is added to the very end of all sheets, which is inconvenient (especially if there are 30+ of them). Is it possible to do something so that the sheet is created to the left or right of the copied sheet .... or, in extreme cases, with the 1st sheet?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Ivanov, 2021-02-09
@Clif1986

Sheets do not move in the Table. They need to be activated.

{
  const sheet2 = sheet.copyTo(ss).setName(sheet2.getResponseText());
  sheet2.activate();
  ss.moveActiveSheet(1);
}

copy and paste on the left
function muFunction() {
  const ui = SpreadsheetApp.getUi();
  const book = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = book.getSheetByName('Шаблон');
  const prompt = ui.prompt('New Sheet Name?');
  if (prompt.getResponseText()) {
    const copy = sheet.copyTo(book).setName(prompt.getResponseText());
    copy.activate();
    book.moveActiveSheet(sheet.getIndex());
  }
}

Put on the right
book.moveActiveSheet(sheet.getIndex()+1);

A
Aleksei Shebanits, 2021-02-09
@shebanits

function copyTo() {
  let ss = SpreadsheetApp.getActiveSpreadsheet();
  let ws = ss.getActiveSheet();
  // по умолчанию копирует справа от активного листа, при -1 слева.
   ss.insertSheet("Тест", ws.getIndex());
  //  ss.insertSheet("Тест", ws.getIndex() - 1);
   SpreadsheetApp.flush();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question