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

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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/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_Custom Function - Fatal编程技术网

Google apps script 使用谷歌应用程序脚本的谷歌工作表:如何编写';状态';是否在返回最终结果之前向单元格发送消息?

Google apps script 使用谷歌应用程序脚本的谷歌工作表:如何编写';状态';是否在返回最终结果之前向单元格发送消息?,google-apps-script,google-sheets,custom-function,Google Apps Script,Google Sheets,Custom Function,我有一个函数,返回输出可能需要一段时间。有没有办法让它先在单元格中打印消息,然后在短时间内用输出覆盖消息?该函数可能需要30秒才能运行,并且可能会在20-30个单元格中使用,因此最好能看到哪个单元格仍在计算,哪个单元格已完成 function do_calc() { print("Now doing the calculations...") // ... (bunch of code here that does the calculations) return

我有一个函数,返回输出可能需要一段时间。有没有办法让它先在单元格中打印消息,然后在短时间内用输出覆盖消息?该函数可能需要30秒才能运行,并且可能会在20-30个单元格中使用,因此最好能看到哪个单元格仍在计算,哪个单元格已完成

function do_calc() {
  print("Now doing the calculations...")

// ... (bunch of code here that does the calculations)

  return output;
}
我试图使用setvalue(),但它说我没有权限在自定义函数中这样做

更新:添加图片

问题: 正如我在评论中所说的,您不能两次
return
,因为第一个
return
语句将抵消后面的代码

  • 此外,自定义函数中不允许使用set方法(如
    setValue
解决方案: 解决方案是合并内置的谷歌表单公式

其中
myFunction
为:

function myFunction() {
  try{
    // some code that delays
    Utilities.sleep(10000);
  }
  catch(e){
    return e.message;
  } 
  return 'Finished';
}
我添加了一个脚本,以确保
返回与脚本相关的错误消息。否则,
iferror
将隐藏它们

小心

自定义函数调用必须在30秒内返回。如果超过此时间,则自定义函数将返回错误:

超过最大执行时间


不会显示,因为您使用了将覆盖错误的
iferror

您不能返回两件事。只有一个或另一个。在调用do之前,可以对当前单元格执行setValue()_calc@Coopersetvalue()在GAS中根本不起作用,所以我不知道怎么做?我在这样的工作表中调用自定义函数:=do_calc()添加了屏幕截图和实际的codeWow@Marios,这是天才!成功了!我勾选了它,它起作用了,但我只是不知道为什么它起作用。它说,只有当函数返回错误时,IFERROR才会执行,但没有错误。@chiwal当函数计算内容时,它会在工作表中返回一些临时错误,这就是我们使用
IFERROR
捕捉它的原因。
=iferror(myFunction(),"Running...")
function myFunction() {
  try{
    // some code that delays
    Utilities.sleep(10000);
  }
  catch(e){
    return e.message;
  } 
  return 'Finished';
}