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
Google apps script 使用doGet()调用不同的函数_Google Apps Script_Google Sheets - Fatal编程技术网

Google apps script 使用doGet()调用不同的函数

Google apps script 使用doGet()调用不同的函数,google-apps-script,google-sheets,Google Apps Script,Google Sheets,下面的脚本是Tanaike提供给我的,作为对我之前发布的问题的回答。它工作得非常好 现在,我需要调用不同的函数,并在用户的电子表格上运行与Admin相同的函数,但我对如何根据函数名或任何其他参数调用它们感到困惑。 现行守则如下: 函数doGet(){ 脚本(); 返回ContentService.createTextOutput(); } 函数保护(){ const url=ScriptApp.getService().getUrl(); FetchApp.fetch(url,{头:{授权:

下面的脚本是Tanaike提供给我的,作为对我之前发布的问题的回答。它工作得非常好

现在,我需要调用不同的函数,并在用户的电子表格上运行与Admin相同的函数,但我对如何根据函数名或任何其他参数调用它们感到困惑。

现行守则如下:

函数doGet(){
脚本();
返回ContentService.createTextOutput();
}
函数保护(){
const url=ScriptApp.getService().getUrl();
FetchApp.fetch(url,{头:{授权:“承载者”+ScriptApp.getOAuthToken()}});
//DriveApp.getFiles()//用于自动检测“”的范围https://www.googleapis.com/auth/drive.readonly。此作用域用于访问令牌。
}
//此脚本与您的“保护”相同。
函数脚本(){
var电子表格=SpreadsheetApp.getActive();
var myValue=SpreadsheetApp.getActiveSheet().getSheetName();
spreadsheet.duplicateActiveSheet();
var totalSheets=countSheets();//脚本函数
myValue=“DO”+总计张数;
SpreadsheetApp.getActiveSpreadsheet().renameActiveSheet(myValue);
var protection=spreadsheet.getActiveSheet().protect();
protection.setUnprotectedRanges([spreadsheet.getRange('C2:E5')、spreadsheet.getRange('C6:D7')、spreadsheet.getRange('F5:G6')、spreadsheet.getRange('B9:G18')、spreadsheet.getRange('G7:G8'))
.removeEditors(['user1.com','user2.com','user3.com']);
电子表格.getRange('G2').setValue(myValue);
电子表格.getRange('G3').setValue(新日期()).setNumberFormat('dd-MMM-YYYY');
spreadsheet.getRange('H1:H').clearContent();
};答案:
您可以通过URL参数实现这一点

更多信息: 调用web应用程序时,您可以在脚本URL末尾以
?key=value
的形式指定URL参数,然后在
doGet(e)
中将这些参数作为属性处理:

然后您可以使用如下功能调用web应用程序:

const url = ScriptApp.getService().getUrl() + "?function=";

// if you want to call function script():
UrlFetchApp.fetch(url + "script", {
                    headers: {
                      authorization: "Bearer " + ScriptApp.getOAuthToken()
                    }
                  });

// if you want to call function altrows():
// remember to also pass the sheet name as a url parameter

var sheetToRunOn = "Sheet1";
UrlFetchApp.fetch(url + "altrows&sheetName=" + sheetToRunOn, {
                    headers: {
                      authorization: "Bearer " + ScriptApp.getOAuthToken()
                    }
                  });
我希望这对你有帮助

参考资料:
答案: 您可以通过URL参数实现这一点

更多信息: 调用web应用程序时,您可以在脚本URL末尾以
?key=value
的形式指定URL参数,然后在
doGet(e)
中将这些参数作为属性处理:

然后您可以使用如下功能调用web应用程序:

const url = ScriptApp.getService().getUrl() + "?function=";

// if you want to call function script():
UrlFetchApp.fetch(url + "script", {
                    headers: {
                      authorization: "Bearer " + ScriptApp.getOAuthToken()
                    }
                  });

// if you want to call function altrows():
// remember to also pass the sheet name as a url parameter

var sheetToRunOn = "Sheet1";
UrlFetchApp.fetch(url + "altrows&sheetName=" + sheetToRunOn, {
                    headers: {
                      authorization: "Bearer " + ScriptApp.getOAuthToken()
                    }
                  });
我希望这对你有帮助

参考资料:

感谢您的回复。我能确认我对你的问题的理解是否正确吗?在您的情况下,电子表格中有两个按钮。您希望使用变通方法为每个按钮运行每个函数。我的理解正确吗?如果我的理解是正确的,现在我想起来了。我认为这个帖子可能对你这种情况有用。谢谢你的回复。我能确认我对你的问题的理解是否正确吗?在您的情况下,电子表格中有两个按钮。您希望使用变通方法为每个按钮运行每个函数。我的理解正确吗?如果我的理解是正确的,现在我想起来了。我认为这条线索可能对你这种情况很有用。谢谢拉法的回答。这个脚本帮助我运行所需的函数。请告诉我是否可以使用altrows()函数传递sheetname。当然,您也可以使用URL参数指定工作表名称。我更新了我的答案。@很高兴能帮上忙!出于文档目的,如果可以,请接受答案(✓) 这对你很有帮助——它也帮助其他将来有同样问题的人找到解决办法:)谢谢拉法的回答。这个脚本帮助我运行所需的函数。请告诉我是否可以使用altrows()函数传递sheetname。当然,您也可以使用URL参数指定工作表名称。我更新了我的答案。@很高兴能帮上忙!出于文档目的,如果可以,请接受答案(✓) 这对你很有帮助——它还可以帮助将来遇到同样问题的其他人找到解决方案:)