Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.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/9/google-apps-script/5.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
Javascript 如何在一个脚本中将函数组合到多个工作表_Javascript_Google Apps Script_Google Sheets - Fatal编程技术网

Javascript 如何在一个脚本中将函数组合到多个工作表

Javascript 如何在一个脚本中将函数组合到多个工作表,javascript,google-apps-script,google-sheets,Javascript,Google Apps Script,Google Sheets,我正在设置一个脚本,在每个谷歌电子表格的A1单元格中输入不同的值。通过在单元格A1中输入csvfeed文件,我需要将csv表中的当前信息绘制到google电子表格中 我尝试过使用下面的代码,但它只更新了一张表,而不是全部 function storeValue() { SpreadsheetApp.getActiveSpreadsheet().getSheetByName('SheetName1').getRange('A1').setValue('=importdata("https://ex

我正在设置一个脚本,在每个谷歌电子表格的A1单元格中输入不同的值。通过在单元格A1中输入csvfeed文件,我需要将csv表中的当前信息绘制到google电子表格中

我尝试过使用下面的代码,但它只更新了一张表,而不是全部

function storeValue() {
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('SheetName1').getRange('A1').setValue('=importdata("https://example.com/csvfeed1.csv")');
}
function storeValue() {
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('SheetName2').getRange('A1').setValue('=importdata("https://example.com/csvfeed2.csv")');
}
function storeValue() {
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('SheetName3').getRange('A1').setValue('=importdata("https://example.com/csvfeed3.csv")');
}
function storeValue() {
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('SheetName4').getRange('A1').setValue('=importdata("https://example.com/csvfeed4.csv")');
}
我也试过:

function storeValue() {
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('SheetName1').getRange('A1').setValue('=importdata("https://example.com/csvfeed1.csv")');
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('SheetName2').getRange('A1').setValue('=importdata("https://example.com/csvfeed2.csv")');
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('SheetName3').getRange('A1').setValue('=importdata("https://example.com/csvfeed3.csv")');
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('SheetName4').getRange('A1').setValue('=importdata("https://example.com/csvfeed4.csv")');
}
脚本运行,但只更新了一张工作表

function storeValue() {
var sh = SpreadsheetApp.getActiveSpreadsheet();

sh.getSheetByName('SheetName1').getRange('A1').setFormula('=importdata("https://example.com/csvfeed1.csv")');
sh.getSheetByName('SheetName2').getRange('A1').setFormula('=importdata("https://example.com/csvfeed2.csv")');
sh.getSheetByName('SheetName3').getRange('A1').setFormula('=importdata("https://example.com/csvfeed3.csv")');
sh.getSheetByName('SheetName4').getRange('A1').setFormula('=importdata("https://example.com/csvfeed4.csv")');
}
编辑:通过一次sh调用进行澄清,并从setvalues更改为setformulas

试试看

function storeValue() {
var sh = SpreadsheetApp.getActiveSpreadsheet();

sh.getSheetByName('SheetName1').getRange('A1').setFormula('=importdata("https://example.com/csvfeed1.csv")');
sh.getSheetByName('SheetName2').getRange('A1').setFormula('=importdata("https://example.com/csvfeed2.csv")');
sh.getSheetByName('SheetName3').getRange('A1').setFormula('=importdata("https://example.com/csvfeed3.csv")');
sh.getSheetByName('SheetName4').getRange('A1').setFormula('=importdata("https://example.com/csvfeed4.csv")');
}

编辑:通过一次sh调用进行澄清,并从setvalues更改为setformulas

由于JS工作的基本原理,第一个根本不起作用(GAS是基于JS的语言)-相同名称下的多个函数将被忽略,并且只运行一个函数(确切顺序不同,但在单个文件全局上下文中,它通常是第一个定义的函数)。第二个应该可以使用,除非您将其与其他
storeValue()
函数一起定义。顺便说一句,请坚持这个概念-从长远来看,它将为您提供良好的服务(例如,为什么要调用
SpreadsheetApp.getActiveSpreadsheet()
4次?将初始值写入变量)Hi-Oleg,感谢您的回复。我不是开发人员,所以我实际上不知道我在做什么。我在另一个主题中找到了要发布到一个单元格的代码,但由于我不熟悉编码,所以我很难让它在多张工作表上工作。我将另一次尝试第二个代码,看看它是否有效。我该如何编写对变量的初始调用?根据Oleg的建议,看一下相关的表单,它应该会为您提供如何使用GASI的基本知识。我刚刚将表单名称1,2,3,4更改为表单1,2,3,4,并在我的网站上使用csv进行了尝试,效果很好。您可能应该使用setFormula,但在我意识到存在setFormula之前,我已经在公式中使用了setValue(),我从来没有遇到过任何问题。也许您试图使用sheetName1,2,3,4作为变量工作表名称,在这种情况下,它们不应该被引用,但将它们作为参数传递或显示全局声明会很有帮助。如果您可以提供一个参数,以便我们可以验证问题,这总是很有帮助的。本网站花费了大量的时间和精力向您展示更好的问题。试着学习如何帮助自己获得更好的答案。由于JS工作的基本原理,第一个根本不起作用(GAS是基于JS的语言)-相同名称下的多个函数将被忽略,只运行一个函数(确切顺序不同,但在单个文件全局上下文中,它通常是第一个定义的)。第二个应该可以使用,除非您将其与其他
storeValue()
函数一起定义。顺便说一句,请坚持这个概念-从长远来看,它将为您提供良好的服务(例如,为什么要调用
SpreadsheetApp.getActiveSpreadsheet()
4次?将初始值写入变量)Hi-Oleg,感谢您的回复。我不是开发人员,所以我实际上不知道我在做什么。我在另一个主题中找到了要发布到一个单元格的代码,但由于我不熟悉编码,所以我很难让它在多张工作表上工作。我将另一次尝试第二个代码,看看它是否有效。我该如何编写对变量的初始调用?根据Oleg的建议,看一下相关的表单,它应该会为您提供如何使用GASI的基本知识。我刚刚将表单名称1,2,3,4更改为表单1,2,3,4,并在我的网站上使用csv进行了尝试,效果很好。您可能应该使用setFormula,但在我意识到存在setFormula之前,我已经在公式中使用了setValue(),我从来没有遇到过任何问题。也许您试图使用sheetName1,2,3,4作为变量工作表名称,在这种情况下,它们不应该被引用,但将它们作为参数传递或显示全局声明会很有帮助。如果您可以提供一个参数,以便我们可以验证问题,这总是很有帮助的。本网站花费了大量的时间和精力向您展示更好的问题。试着学习如何帮助自己获得更好的答案。这是如何回答问题的?OP发布的代码有哪些相关更改?这是如何回答问题的?OP发布的代码有哪些相关更改?