Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 如何读取和存储单元格值以在脚本中使用?_Google Apps Script_Google Sheets_Google Calendar Api - Fatal编程技术网

Google apps script 如何读取和存储单元格值以在脚本中使用?

Google apps script 如何读取和存储单元格值以在脚本中使用?,google-apps-script,google-sheets,google-calendar-api,Google Apps Script,Google Sheets,Google Calendar Api,我有一个脚本,可以从日历中检索所有事件,并将它们放在工作表中。我正试图通过引用活动工作表中的单元格B2使此脚本成为动态的,这样我就可以更改日历ID,而无需每次跳入脚本工具 原始代码: var mycal = "IDNUMBERHERE@group.calendar.google.com"; var cal = CalendarApp.getCalendarById(mycal); var events = cal.getEvents(new Date("January 1, 2017

我有一个脚本,可以从日历中检索所有事件,并将它们放在工作表中。我正试图通过引用活动工作表中的单元格B2使此脚本成为动态的,这样我就可以更改日历ID,而无需每次跳入脚本工具

原始代码:

var mycal = "IDNUMBERHERE@group.calendar.google.com"; 

var cal = CalendarApp.getCalendarById(mycal);   

var events = cal.getEvents(new Date("January 1, 2017 23:59:59 CST"), new Date("January 1, 2018 23:59:59 CST"));
新代码:

var cal = CalendarApp.getCalendarById(SpreadsheetApp.getActiveSheet().getRange("B2").getValue()); 

var events = cal.getEvents(new Date("January 1, 2017 23:59:59 CST"), new Date("January 1, 2018 23:59:59 CST"));

运行新代码时,我不断收到错误“TypeError:Cannotcallmethod”getEvents“of null”。我猜这与我没有正确设置“var-cal”有关。有什么想法吗?

我也有同样的问题。尝试用getDisplayValue()更改getValue()

对于任何好奇的人,只需执行以下操作即可解决此问题

  var mycal = SpreadsheetApp.getActiveSheet().getRange("c2").getValue(); // PULL CALENDAR ID FROM CELL
  var cal = CalendarApp.getCalendarById(mycal);

使用Logger.log(SpreadsheetApp.getActiveSheet().getRange(“B2”).getValue());要检查您得到的值是否实际是日历ID,您可以使用如下提示('calendar ID','Enter calendar ID',SpreadsheetApp.getUi().ButtonSet.OK)当我将自己的日历ID替换到测试表中时,您的代码对我来说运行良好。我还建议您研究用于存储文档变量的PropertiesService: