Clear, copy and paste from one file to another using google script

Viewed 31

I have this script running from one source file to several other files where I use the data. The script has always tended to work fine but recently started getting the error

Exception: Service Spreadsheets timed out while accessing document with id.

I only get the error when the script is running for a particular destination file, as opposed to others. It also happens that this destination file is much larger than the others (maybe the problem). Anyway, the script is below and wondering if anyone has any alternative suggestions or feedback.

function Function_name() {

  var ss=SpreadsheetApp.getActiveSpreadsheet()
  var sourcesheet=ss.getSheetByName("RAWFINEX")
  var data=sourcesheet.getDataRange().getValues()

  var destinationfile=SpreadsheetApp.openById("Sheet ID")
  var destinationsheet=destinationfile.getSheetByName("RAWFINEX")
  var destinationlr=lastUsableRow(destinationsheet)
  var destinationlc=destinationsheet.getLastColumn
  var existingdata=destinationsheet.getRange(1,1,destinationlr,5).clearContent

  var destinationrange=destinationsheet.getRange(1,1,data.length,data[0].length)
  var pastedata=destinationrange.setValues(data)
  
}
1 Answers

Copy Data And Log

function CopyDataAndLog() {
  var log = DriveApp.getFileById("logid");
  var ss=SpreadsheetApp.getActive();
  var ssh=ss.getSheetByName("RAWFINEX");
  var svs=ssh.getDataRange().getValues();
  var ss=SpreadsheetApp.openById("Sheet ID")
  var fi=DriveApp.getFileById(ss.getId())
  log.setContent(log.getBlob().getDataAsString() + `${fi.getSize()},${file.getName()},${file.getId()},${file.getType()}${'\r\n'}}`;
  var tsh=ss.getSheetByName("RAWFINEX")
  var tlr=lastUsableRow(tsh)
  tsh.getRange(1,1,tlr,5).clearContent();
  tsh.getRange(1,1,svs.length,svs[0].length).trg.setValues(svs);
}
Related