0

I am using Google Script to copy content of one Google Sheet to another sheet. I am using the following code.

function copySheet()
{
  // open the spreadsheets where the data will be stored
  var sourceTable = SpreadsheetApp.openById("SheetID"); // source spreadsheet
  var srcSheet = sourceTable.getSheetByName("RawData");
  var targetTable = SpreadsheetApp.openById("SheetID"); // target spreadsheet
  var tarSheet = targetTable.getSheetByName("Sheet3");

  // get the last row in the source sheet
  var lastRowSource = srcSheet.getLastRow();
  var lastCol = "L";

  //read the source into an array

  var aSrc = srcSheet.getRange("A1:" + lastCol + lastRowSource).getValues();
  //save src array to destination
  tarSheet.getRange("A1:" + lastCol + lastRowSource).setValues(aSrc);
}

The Script ran perfectly the first time but now is throwing the Service Time Out Error. Is there a way I can make it more efficient? The data is somewhat large ( around 25000 rows and 12 columns).

VSR
  • 87
  • 2
  • 18
  • That seems pretty efficient to me. I would consider doing it in [batches](https://developers.google.com/apps-script/guides/support/best-practices#use_batch_operations) using a timebased trigger. – Cooper Jan 18 '19 at 20:17
  • This might be helpful [https://stackoverflow.com/a/27328517/7215091](https://stackoverflow.com/a/27328517/7215091) – Cooper Jan 18 '19 at 20:27
  • There are reports about `getLastRow()` taking too much time to execute. Try another method like using `sheet.getDataRange().getValues().length` – Rubén Jan 18 '19 at 21:32
  • You may try 1. copy whole sheet to destination spreadsheet 2. in destination spreadsheet use `Range.copyTo()` to copy range from copied sheet to destination sheet. – Kos Jan 18 '19 at 22:19
  • @Cooper [Issue 6102](https://code.google.com/p/google-apps-script-issues/issues/detail?id=6102): getLastRow() sometimes requires extremely high runtime – Rubén Jan 18 '19 at 22:44
  • Provide Execution Transcript. – TheMaster Jan 19 '19 at 00:48

0 Answers0