尝试在应用程序脚本中使用 importrange 时参数列表后缺少 )

Missing ) after argument list when trying to use importrange in app script

我正在尝试在 google 工作表中使用 google 应用程序脚本来设置导入范围的公式。我有以下代码:

SpreadsheetApp.getActiveSheet().getCurrentCell().setFormulaR1C1('=IMPORTRANGE("https://docs.google.com/spreadsheets/d/blahblah","Completed Work!B5:N")');

我的问题是当我试图让代码接受 URL 的参数时,所以当我说:

SpreadsheetApp.getActiveSheet().getCurrentCell().setFormulaR1C1('=IMPORTRANGE('URL',"Completed Work!B5:N")');

当我尝试运行我的函数时使用:

myfunction("https://docs.google.com/spreadsheets/d/blahblah","Completed Work!B5:N")

我在参数列表错误后得到一个 Missing ),我将如何修复第二个公式以便 URL 可以作为函数的参数传递?

谢谢!

简答

使用JavaScript字符串连接运算符+

扩展答案

您必须了解 JavaScript 上的字符串连接。

来自https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String(跟随link看到内联links)

Strings are useful for holding data that can be represented in text form. Some of the most-used operations on strings are to check their length, to build and concatenate them using the + and += string operators, checking for the existence or location of substrings with the indexOf() method, or extracting substrings with the substring() method.

而不是

'=IMPORTRANGE('URL',"Completed Work!B5:N")'

使用

'=IMPORTRANGE("' + URL + '","Completed Work!B5:N")'

注意:请注意使用 " 双引号括起 URL 的值,因为它是 IMPORTRANGE 所要求的。