简体   繁体   中英

Google Scripts - Function Error with Time Trigger

This function/trigger combo is intended to keep a log of live data on a daily basis. The intent is that N2:P2 get pasted as values at the bottom of Columns C:E.

  1. pullvalue() works fine if I run it manually.
  2. HERE'S THE ISSUE: Setting up a time-driven trigger causes the values to be pasted in at C1:E1 instead of at the bottom of the columns.
  3. My suspicion is that target_range is breaking down when Clast returns a zero value, but I can't figure out why.

What am I missing? Thanks for your help/feedback!

 function pullvalue() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var target = SpreadsheetApp.getActiveSpreadsheet(); var source_sheet = ss.getSheetByName("Production"); var target_sheet = target.getSheetByName("Production"); var source_range = source_sheet.getRange("N2:P2"); var Cvals = ss.getRange("C1:C").getValues(); var Clast = Cvals.filter(String).length; var target_range = target_sheet.getRange("C"+(Clast+1)+":E"+(Clast+1)) source_range.copyTo(target_range, {contentsOnly:true});} 

Please verify this line:

var Cvals = ss.getRange("C1:C").getValues();

variable ss return currently activespreadsheet only, but when it runs from a time-driven trigger there is no active sheet, so you need to indicate the sheet, you can use getSheetByName() or getSheets() the line should look like this:

var sheet = ss.getSheetByName("Sheet1");
var Cvals = sheet.getRange("C1:C").getValues();

Hope this will help you.

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM