G
G
Graun2020-02-03 10:44:43
Google Apps Script
Graun, 2020-02-03 10:44:43

How to execute a script on all sheets of a table?

Help to correct the script so that it does the same but cuts data from all sheets expressed and inserted into another table on sheet "Sheet3"

function copyTabList() {
  var from = SpreadsheetApp.getActiveSheet();
  var fromValues = from
    .getDataRange()
    .offset(1, 0)
    .getValues();
  var fromData = fromValues;

  var tss = SpreadsheetApp.openById('Id');
  var ts = tss.getSheetByName('Sheet3');

  ts.getDataRange()
    .offset(1, 0)
    .clearContent()
    .getSheet()
    .getRange(2, 1, fromData.length, fromData[0].length)
    .setValues(fromData);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Grigory Boev, 2020-02-05
@ProgrammerForever

You can get a list of all sheets and loop through. Copy the data in the same way, but change the line number when inserting (here 2 is replaced by (last line of data on the sheet + 1))

.getRange(2, 1, FromData.length, FromData[0].length)

If you want it to work faster, then you need to form a two-dimensional array of data from all, and then write to the cells at a time. Just add them line by line to a common two-dimensional array when you receive a new portion of data.
Even easier is to use IMPORTRANGE() - everything is pulled from another table there. If you need to write in a column, you can use the array declaration:
={1;2;3}
... will print 1,2,3 in a column. Replace the numbers with formulas with IMPORTRANGE() by the number of sheets and automatically get what you want. The only BUT - the data is read-only

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question