Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.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
Object 谷歌应用程序脚本对象混淆_Object_Google Apps Script - Fatal编程技术网

Object 谷歌应用程序脚本对象混淆

Object 谷歌应用程序脚本对象混淆,object,google-apps-script,Object,Google Apps Script,上面的代码将adsf插入电子表格,而不是其他内容,但是下面的代码按预期记录了其他内容。。。这是怎么回事?将应用程序脚本函数设置为myFunction。。。(附言:我删除了之前的问题,并用这个问题更新了我的问题) 此代码未按预期工作: var obj = {variable:"adsf"}; function myFunction() { function scheduledScript(plusMonth,plusDay,plusHour,plusMinute) { obj.var

上面的代码将
adsf
插入电子表格,而不是
其他内容
,但是下面的代码按预期记录了
其他内容
。。。这是怎么回事?将应用程序脚本函数设置为myFunction。。。(附言:我删除了之前的问题,并用这个问题更新了我的问题)

此代码未按预期工作:

var obj = {variable:"adsf"};

function myFunction() {
  function scheduledScript(plusMonth,plusDay,plusHour,plusMinute) {
    obj.variable = "something else";
    var now = new Date();
    var year = now.getFullYear();
    var MONTH = now.getMonth();
    var DAY = now.getDate();
    var HOUR = now.getHours();
    var MINUTE = now.getMinutes();
    var seconds = now.getSeconds();
    var milliseconds = now.getMilliseconds();
    var date = new Date(year,MONTH+plusMonth,DAY+plusDay,HOUR+plusHour,MINUTE+plusMinute,10,0);
    ScriptApp.newTrigger('send').timeBased().at(date).create();
  }
  scheduledScript(0,0,0,1);
}

function send() {
  Logger.log(obj.variable); //"adsf"      
  SpreadsheetApp.openById("1z28v6Y_4kwTxfqXLvEb1Zo81QJGZ8AfxkoSFYP5LM8E").setActiveSelection("H1").setValue(obj.variable);
}
var obj = {variable:"asdf"};

function myFunction() {
  function process() {
    Logger.log(obj.variable); //"asdf"
    obj.variable = "something else";
    notify();
  }
  process();
}

function notify () {
  Logger.log(obj.variable); //"something else"
}
此代码按预期工作:

var obj = {variable:"adsf"};

function myFunction() {
  function scheduledScript(plusMonth,plusDay,plusHour,plusMinute) {
    obj.variable = "something else";
    var now = new Date();
    var year = now.getFullYear();
    var MONTH = now.getMonth();
    var DAY = now.getDate();
    var HOUR = now.getHours();
    var MINUTE = now.getMinutes();
    var seconds = now.getSeconds();
    var milliseconds = now.getMilliseconds();
    var date = new Date(year,MONTH+plusMonth,DAY+plusDay,HOUR+plusHour,MINUTE+plusMinute,10,0);
    ScriptApp.newTrigger('send').timeBased().at(date).create();
  }
  scheduledScript(0,0,0,1);
}

function send() {
  Logger.log(obj.variable); //"adsf"      
  SpreadsheetApp.openById("1z28v6Y_4kwTxfqXLvEb1Zo81QJGZ8AfxkoSFYP5LM8E").setActiveSelection("H1").setValue(obj.variable);
}
var obj = {variable:"asdf"};

function myFunction() {
  function process() {
    Logger.log(obj.variable); //"asdf"
    obj.variable = "something else";
    notify();
  }
  process();
}

function notify () {
  Logger.log(obj.variable); //"something else"
}
没有混乱


您的第一个脚本示例设置了一个基于时间的触发器,该触发器执行
send()
函数。当它执行时,它将获得在代码的第一行中设置的
obj
全局变量值。触发器不会运行更改全局变量值的
myFunction()
函数。因此,您可以看到在电子表格单元格中输入的
adsf

我很困惑。。。之所以调用触发器,是因为myFunction在最后一行调用了它。。。这意味着在触发器执行器在其自己的会话空间中运行之前,所有内容都将被更新,与任何用户会话分离。
myFunction()
函数设置
obj
变量的新值,然后创建触发器。但是当触发器执行时,它是一个单独的会话,是脚本的新执行,因此它会获得原始的
obj
变量值。