如何从 google sheet 绑定脚本调用 Web 应用程序以修改 sheet
How to call web app from a google sheet bound script to modify the sheet
我正在尝试制作一个可以由 Google sheet 的绑定脚本触发的 WebApp,以在我的权限下修改 sheet。我试过使用可安装的触发器,但我需要比它们提供的更多的功能。
我从
并试图使其正常运行,但一直出现相同的错误 Exception: Request failed for https://script.google.com returned code 401. Truncated server response:
,然后是页面的 HTML。我什至难以获得此工作的基本版本,上面的 link 是我找到的 most/only 相关指南。
下面是我尝试获取基本功能的尝试(它 returns 与上述相同的错误)。如果我在浏览器中打开网络应用程序,它会成功地将 1 添加到第一个单元格,但我不知道如何从 sheet 的绑定脚本中正确调用网络应用程序。
来自传播sheet的绑定脚本:
function myFunction() {
UrlFetchApp.fetch('web app url');
}
来自网络应用:
function doGet() {
var ss = SpreadsheetApp.openById('spreadsheet id');
var sheet = ss.getSheetByName('Sheet1');
var cell = sheet.getRange(1, 1);
cell.setValue(cell.getValue() + 1);
}
我只需要一个功能示例。在那之后,我可以从那里拿走它。
试试这个:
这是以我的身份运行的,只有我可以访问它。所以不妨修改一下。
function myFunction() {
Logger.log(ScriptApp.getService().getUrl());
var url=ScriptApp.getService().getUrl();
var params = {
method: "get",
headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
};
UrlFetchApp.fetch(url,params);
}
function doGet() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Sheet1');
sh.getRange("A1").setValue(sh.getRange("A1").getValue() + 1);
var html=Utilities.formatString('Process Complete: Current Value: %s',sh.getRange("A1").getValue());
return HtmlService.createHtmlOutput(html);
}
这些是我添加的范围:
"oauthScopes": ["https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/script.external_request", "https://www.googleapis.com/auth/spreadsheets"]
我正在尝试制作一个可以由 Google sheet 的绑定脚本触发的 WebApp,以在我的权限下修改 sheet。我试过使用可安装的触发器,但我需要比它们提供的更多的功能。
我从
Exception: Request failed for https://script.google.com returned code 401. Truncated server response:
,然后是页面的 HTML。我什至难以获得此工作的基本版本,上面的 link 是我找到的 most/only 相关指南。
下面是我尝试获取基本功能的尝试(它 returns 与上述相同的错误)。如果我在浏览器中打开网络应用程序,它会成功地将 1 添加到第一个单元格,但我不知道如何从 sheet 的绑定脚本中正确调用网络应用程序。
来自传播sheet的绑定脚本:
function myFunction() {
UrlFetchApp.fetch('web app url');
}
来自网络应用:
function doGet() {
var ss = SpreadsheetApp.openById('spreadsheet id');
var sheet = ss.getSheetByName('Sheet1');
var cell = sheet.getRange(1, 1);
cell.setValue(cell.getValue() + 1);
}
我只需要一个功能示例。在那之后,我可以从那里拿走它。
试试这个:
这是以我的身份运行的,只有我可以访问它。所以不妨修改一下。
function myFunction() {
Logger.log(ScriptApp.getService().getUrl());
var url=ScriptApp.getService().getUrl();
var params = {
method: "get",
headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
};
UrlFetchApp.fetch(url,params);
}
function doGet() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Sheet1');
sh.getRange("A1").setValue(sh.getRange("A1").getValue() + 1);
var html=Utilities.formatString('Process Complete: Current Value: %s',sh.getRange("A1").getValue());
return HtmlService.createHtmlOutput(html);
}
这些是我添加的范围:
"oauthScopes": ["https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/script.external_request", "https://www.googleapis.com/auth/spreadsheets"]