调用并 运行 doPost
Call and Run doPost
是否可以调用同一个项目中存在的doPost。当我在同一个项目中调用 doPost 时,我得到一个 200 响应代码,但是如果我让其他人使用 运行 相同的函数,他们会得到一个 500 响应代码。我正在使用 dev URL,以我的身份执行应用程序,我域中的任何人都可以访问该应用程序。这是我的 sample file.
function sendPost(){
var sheetURL = SpreadsheetApp.getActiveSpreadsheet().getUrl();
var webAppUrl = "<<my dev url>>"; //insert webapp URL of Dev or Exe script file
var auth = ScriptApp.getOAuthToken();
var header = { 'Authorization': 'Bearer ' + auth};
var payload = {scriptName : 'updateDataV2', sheetURL : sheetURL};
var options = {
method : 'post',
headers : header,
muteHttpExceptions : true,
payload : payload
};
var resp = UrlFetchApp.fetch(webAppUrl, options);
Logger.log(resp.getResponseCode());
var message = resp.getResponseCode() == 200 ? 'Database update complete' : 'There seems to be an issue. Please try again.';
//if(resp.getResponseCode() == 200){resetFormulas();}
return SpreadsheetApp.getActiveSpreadsheet().toast(message);
//Below 2 lines needed to setup access
//DriveApp.getFiles();
//DriveApp.createFile(blob);
}
function doPost(e){
if(!e.parameters.scriptName){return ContentService.createTextOutput('A function name must be passed in a payload to run.');};
Logger.log('parameters from caller ' + JSON.stringify(e));
var newText = this[e.parameters.scriptName](e); //This line runs the function being called with parameters
return ContentService.createTextOutput(newText);
}
这是给用户的错误信息。
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="description" content="Web word processing, presentations and spreadsheets">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
<link rel="shortcut icon" href="//docs.google.com/favicon.ico">
<title>Error</title>
<meta name="referrer" content="origin">
<link href="//fonts.googleapis.com/css?family=Product+Sans" rel="stylesheet" type="text/css">
<style>/* Copyright 2020 Google Inc. All Rights Reserved. */
.goog-inline-block{position:relative;display:-moz-inline-box;display:inline-block}* html .goog-inline-block{display:inline}*:first-child+html .goog-inline-block{display:inline}#drive-logo{margin:18px 0;position:absolute;white-space:nowrap}.docs-drivelogo-img{background-image:url('//ssl.gstatic.com/images/branding/googlelogo/1x/googlelogo_color_116x41dp.png');background-size:116px 41px;display:inline-block;height:41px;vertical-align:bottom;width:116px}.docs-drivelogo-text{color:#000;display:inline-block;opacity:0.54;text-decoration:none;font-family:'Product Sans',Arial,Helvetica,sans-serif;font-size:32px;text-rendering:optimizeLegibility;position:relative;top:-6px;left:-7px;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}@media (-webkit-min-device-pixel-ratio:1.5),(min-resolution:144dpi){.docs-drivelogo-img{background-image:url('//ssl.gstatic.com/images/branding/googlelogo/2x/googlelogo_color_116x41dp.png')}}
</style>
<style type="text/css">body {background-color: #fff; font-family: Arial,sans-serif; font-size: 13px; margin: 0; padding: 0;}a, a:link, a:visited {color: #112ABB;}</style>
<style type="text/css">.errorMessage {font-size: 12pt; font-weight: bold; line-height: 150%;}</style>
</head>
<body>
<div id="outerContainer">
<div id="innerContainer">
<div style="position: absolute; top: -80px;">
<div id="drive-logo"><a href="/"><span class="docs-drivelogo-img" title="Google logo"></span><span class="docs-drivelogo-text"> Drive</span></a></div>
</div>
<p style="padding-top: 15px">Google Docs encountered an error. Please try reloading this page, or coming back to it in a few minutes.</p>
<p>To learn more about the Google Docs editors, please visit our <a href="https://support.google.com/docs/?hl=en&p=error_help" target="_blank">help center</a>.</p>
<p><br><b>We're sorry for the inconvenience.</b><br><i>- The Google Docs Team</i></p>
</div>
</div>
</body>
<style>html {height: 100%; overflow: auto;}body {height: 100%; overflow: auto;}#outerContainer {margin: auto; max-width: 750px;}#innerContainer {margin-bottom: 20px; margin-left: 40px; margin-right: 40px; margin-top: 80px; position: relative;}</style>
</html>
好的。
我调试了你的代码。我看到了下一行。
var newText = this[e.parameters.scriptName](e);
很有意思。当您调用不同用户的网络应用程序时,它也可能指向不同的东西。
在这种情况下不要使用 this
。尝试将其覆盖为字典或 IIFE。简而言之,您应该确保您的函数想要 运行 的所有代码都已经初始化。这在当前上下文中不起作用。
我复制了文件然后注释了下一行
// var newText = this[e.parameters.scriptName](e);
然后我重新发布了该应用程序,现在它可以正常工作了。
额外
您需要为所有授权用户发布应用程序
或所有匿名
是否可以调用同一个项目中存在的doPost。当我在同一个项目中调用 doPost 时,我得到一个 200 响应代码,但是如果我让其他人使用 运行 相同的函数,他们会得到一个 500 响应代码。我正在使用 dev URL,以我的身份执行应用程序,我域中的任何人都可以访问该应用程序。这是我的 sample file.
function sendPost(){
var sheetURL = SpreadsheetApp.getActiveSpreadsheet().getUrl();
var webAppUrl = "<<my dev url>>"; //insert webapp URL of Dev or Exe script file
var auth = ScriptApp.getOAuthToken();
var header = { 'Authorization': 'Bearer ' + auth};
var payload = {scriptName : 'updateDataV2', sheetURL : sheetURL};
var options = {
method : 'post',
headers : header,
muteHttpExceptions : true,
payload : payload
};
var resp = UrlFetchApp.fetch(webAppUrl, options);
Logger.log(resp.getResponseCode());
var message = resp.getResponseCode() == 200 ? 'Database update complete' : 'There seems to be an issue. Please try again.';
//if(resp.getResponseCode() == 200){resetFormulas();}
return SpreadsheetApp.getActiveSpreadsheet().toast(message);
//Below 2 lines needed to setup access
//DriveApp.getFiles();
//DriveApp.createFile(blob);
}
function doPost(e){
if(!e.parameters.scriptName){return ContentService.createTextOutput('A function name must be passed in a payload to run.');};
Logger.log('parameters from caller ' + JSON.stringify(e));
var newText = this[e.parameters.scriptName](e); //This line runs the function being called with parameters
return ContentService.createTextOutput(newText);
}
这是给用户的错误信息。
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="description" content="Web word processing, presentations and spreadsheets">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
<link rel="shortcut icon" href="//docs.google.com/favicon.ico">
<title>Error</title>
<meta name="referrer" content="origin">
<link href="//fonts.googleapis.com/css?family=Product+Sans" rel="stylesheet" type="text/css">
<style>/* Copyright 2020 Google Inc. All Rights Reserved. */
.goog-inline-block{position:relative;display:-moz-inline-box;display:inline-block}* html .goog-inline-block{display:inline}*:first-child+html .goog-inline-block{display:inline}#drive-logo{margin:18px 0;position:absolute;white-space:nowrap}.docs-drivelogo-img{background-image:url('//ssl.gstatic.com/images/branding/googlelogo/1x/googlelogo_color_116x41dp.png');background-size:116px 41px;display:inline-block;height:41px;vertical-align:bottom;width:116px}.docs-drivelogo-text{color:#000;display:inline-block;opacity:0.54;text-decoration:none;font-family:'Product Sans',Arial,Helvetica,sans-serif;font-size:32px;text-rendering:optimizeLegibility;position:relative;top:-6px;left:-7px;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}@media (-webkit-min-device-pixel-ratio:1.5),(min-resolution:144dpi){.docs-drivelogo-img{background-image:url('//ssl.gstatic.com/images/branding/googlelogo/2x/googlelogo_color_116x41dp.png')}}
</style>
<style type="text/css">body {background-color: #fff; font-family: Arial,sans-serif; font-size: 13px; margin: 0; padding: 0;}a, a:link, a:visited {color: #112ABB;}</style>
<style type="text/css">.errorMessage {font-size: 12pt; font-weight: bold; line-height: 150%;}</style>
</head>
<body>
<div id="outerContainer">
<div id="innerContainer">
<div style="position: absolute; top: -80px;">
<div id="drive-logo"><a href="/"><span class="docs-drivelogo-img" title="Google logo"></span><span class="docs-drivelogo-text"> Drive</span></a></div>
</div>
<p style="padding-top: 15px">Google Docs encountered an error. Please try reloading this page, or coming back to it in a few minutes.</p>
<p>To learn more about the Google Docs editors, please visit our <a href="https://support.google.com/docs/?hl=en&p=error_help" target="_blank">help center</a>.</p>
<p><br><b>We're sorry for the inconvenience.</b><br><i>- The Google Docs Team</i></p>
</div>
</div>
</body>
<style>html {height: 100%; overflow: auto;}body {height: 100%; overflow: auto;}#outerContainer {margin: auto; max-width: 750px;}#innerContainer {margin-bottom: 20px; margin-left: 40px; margin-right: 40px; margin-top: 80px; position: relative;}</style>
</html>
好的。
我调试了你的代码。我看到了下一行。
var newText = this[e.parameters.scriptName](e);
很有意思。当您调用不同用户的网络应用程序时,它也可能指向不同的东西。
在这种情况下不要使用 this
。尝试将其覆盖为字典或 IIFE。简而言之,您应该确保您的函数想要 运行 的所有代码都已经初始化。这在当前上下文中不起作用。
我复制了文件然后注释了下一行
// var newText = this[e.parameters.scriptName](e);
然后我重新发布了该应用程序,现在它可以正常工作了。
额外
您需要为所有授权用户发布应用程序
或所有匿名