GAS withSuccessHandler为某些功能而不是其他功能工作

时间:2017-09-10 01:31:01

标签: javascript google-apps-script google-sheets

我已将此项目附加到电子表格中:

中将Html.HTML:

<!DOCTYPE html>
<html>
  <head>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
   <script>
function script2() {
    google.script.run.doSomething();
}
function RunUpdateYearsManual() {
google.script.run.withSuccessHandler(script2).resetIndicatorsUpdateYears();
}
   </script>
  </head>
  <body>
 <input type="button" value="Update" onClick="RunUpdateYearsManual();" />
 <br><p></p>

  </body>
</html>

Code.gs:

function doSomething() {
  Logger.log('I was called! CALLED');
}

function resetIndicatorsUpdateYears() {
var newD = new Date();
var hour = newD.getHours();
var minute = newD.getMinutes();
var second = newD.getSeconds();
var s3 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Intro")
s3.getRange("F29").setNote("Started last update cycle on: "+ newD);
s3.getRange("F31:F46").setValue("Skipped").clearFormat();
s3.getRange("F30").setValue("Waiting").clearFormat();
s3.getRange("F30").setNote("Waiting for Year and English Lists Update.\n\n\n"+"Started at: "+hour+":"+minute+":"+second); 
s3.getRange("F31").setNote("Skipped Company Lists Update 0001-0350.\n\n\n"+"Started at: "+hour+":"+minute+":"+second); 
s3.getRange("F32").setNote("Skipped Company Lists Update 0351-0700.\n\n\n"+"Started at: "+hour+":"+minute+":"+second); 
s3.getRange("F33").setNote("Skipped Company Lists Update 0701-1050.\n\n\n"+"Started at: "+hour+":"+minute+":"+second); 
s3.getRange("F34").setNote("Skipped Company Lists Update 1051-1400.\n\n\n"+"Started at: "+hour+":"+minute+":"+second); 
s3.getRange("F35").setNote("Skipped Company Lists Update 1401-XXXX.\n\n\n"+"Started at: "+hour+":"+minute+":"+second); 
s3.getRange("F36").setNote("Skipped Bundled Games In Year and English Lists Update.\n\n\n"+"Started at: "+hour+":"+minute+":"+second); 
s3.getRange("F37").setNote("Skipped Bundled Games In Company Lists Removal 0001-0600.\n\n\n"+"Started at: "+hour+":"+minute+":"+second); 
s3.getRange("F38").setNote("Skipped Bundled Games In Company Lists Removal 0601-1200.\n\n\n"+"Started at: "+hour+":"+minute+":"+second); 
s3.getRange("F39").setNote("Skipped Bundled Games In Company Lists Removal 1201-XXXX.\n\n\n"+"Started at: "+hour+":"+minute+":"+second); 
s3.getRange("F40").setNote("Skipped Empty Rows Sorting 0001-0400.\n\n\n"+"Started at: "+hour+":"+minute+":"+second); 
s3.getRange("F41").setNote("Skipped Empty Rows Sorting 0401-0800.\n\n\n"+"Started at: "+hour+":"+minute+":"+second); 
s3.getRange("F42").setNote("Skipped Empty Rows Sorting 0801-1200.\n\n\n"+"Started at: "+hour+":"+minute+":"+second); 
s3.getRange("F43").setNote("Skipped Empty Rows Sorting 1201-1600.\n\n\n"+"Started at: "+hour+":"+minute+":"+second); 
s3.getRange("F44").setNote("Skipped Empty Rows Sorting 1601-XXXX.\n\n\n"+"Started at: "+hour+":"+minute+":"+second); 
s3.getRange("F45").setNote("Skipped Empty Rows Removal 0001-0900.\n\n\n"+"Started at: "+hour+":"+minute+":"+second); 
s3.getRange("F46").setNote("Skipped Empty Rows Removal 0900-XXXX.\n\n\n"+"Started at: "+hour+":"+minute+":"+second); 
deleteFormulas();
  Logger.log('I was called! TEST');
}

但这似乎不起作用,我运行RunUpdateYearsManual并且它所做的只是执行第一个函数而不启动第二个,记录器只会返回“我被调用!TEST”,没有失败并且使用withFailureHandler没有帮助

但是这里有关于它的奇怪部分,如果我删除那些在我的代码中编辑F30:F46的行并使其成为这样:

Code.gs:

function doSomething() {
  Logger.log('I was called! CALLED');
}

function resetIndicatorsUpdateYears() {
var newD = new Date();
var hour = newD.getHours();
var minute = newD.getMinutes();
var second = newD.getSeconds();
var s3 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Intro")
s3.getRange("F29").setNote("Started last update cycle on: "+ newD);
  Logger.log('I was called! TEST');
}

这现在有效,我在记录器中看到“我被叫了!被叫”。

所以我完全迷失在这里。我想可能是原始脚本更长的某种限制?但是运行起来只需不到20秒,我能找到的唯一文档是自定义函数运行时限制为30秒/执行,但不认为这是这种情况。

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:0)

以下修改怎么样?在此修改后的脚本中,我尝试使用get_rid_of_us = ('.', '"', '?', ' ') def fix_me(text): filter(lambda i: i not in get_rid_of_us,text) setNotes()来缩短处理时间。请尝试这个修改过的样本。

resetIndicatorsUpdateYears()的详细信息为here

修改后的脚本:

setNotes()

答案 1 :(得分:0)

在Google论坛(https://plus.google.com/111489556118142836691/posts/53tX2fW6D6M)的用户帮助下,我设法找到并解决了问题。

基本上,您需要将用于执行功能的UI窗口保持打开状态,一个窗口显然会阻止您在电子表格中正常工作,因此侧边栏UI也可以正常工作,您用来触发的UI第一个功能只需要保持开放。 如果您在任何时候关闭该UI,则当前功能将完成运行但不会触发任何其他功能。

不完全确定为什么会这样,但这似乎解决了问题。