在Google Apps脚本中,使用REST API调用脚本中的函数的简单方法是什么?我知道REST的动词(GET,POST,UPDATE,DELETE)需要一些关于如何将功能映射到JavaScript方法的决定,但我正在寻找最通用的方式来说“给我一个REST接口为此方法“。Google Apps脚本:将方法设置为公共REST界面的简单方法
0
A
回答
2
听起来像是你想ContentService。你为你想要的动词编写一个函数(GET为doGet
,POST为doPost
,其他不支持),然后你可以通过将其名称作为参数委托给另一个函数。
1
最简单的方法是编写一个接受参数的Web应用程序,将它们传递给函数,然后返回函数的输出。 (如科里说......但我已经写了这一切的代码,所以在这里你去!)
这正是如此,一个简单的计算器功能:
/**
* GET requests should be for read-only queries; they should not change the state of the server and its data.
*/
function doGet(e) {
var output = '';
if (Object.keys(e.parameters).length < 3) {
output += doMath('help','0','0');
}
else {
output += JSON.stringify(e.parameters) +'\n'; // Echo parameters - debug only
output += doMath(e.parameters.operation[0], e.parameters.val1[0], e.parameters.val2[0]);
}
return ContentService
.createTextOutput(output)
.setMimeType(ContentService.MimeType.JSON);
}
如果你想成为一个完整的极简主义,这将工作,没有错误处理:
function doGet(e) {
return ContentService
.createTextOutput(doMath(e.parameters.operation[0], e.parameters.val1[0], e.parameters.val2[0]))
.setMimeType(ContentService.MimeType.JSON);
}
为了有效在这个模型中,你的目标函数需要处理自己的错误处理和返回的东西在所有情况下是有用的。下面是一个简单的计算器:
function doMath(operation, val1, val2) {
// Do error checking for parameters
var errors = '';
for (var arg in arguments) {
if (arguments[arg] == 'undefined') errors += "Missing " + arg + '\n';
if (arg.indexOf('val') !== -1) {
if (isNaN(arguments[arg])) errors += "Not a number: " + arg + '\n';
}
}
// If we found errors, just report them
if (errors != '') return errors;
// Body of function
var result;
var num1 = parseFloat(val1);
var num2 = parseFloat(val2);
switch (operation) {
case 'add':
result = num1 + num2;
break;
case 'sub':
result = num1 - num2;
break;
case 'mult':
result = num1 * num2;
break;
case 'div':
if (num2 !== 0.0) {
result = num1/num2;
}
else {
result = 'Divide by zero';
}
break;
case 'help':
result = "Example:\n\n"
+ " "+ScriptApp.getService().getUrl()
+ "?operation=add&val1=2&val2=2\n\n"
+ "Returns '4'";
break;
default:
result = 'Unsupported operation';
break;
}
return result;
}
在部署您的Web应用程序,记得将其提供给“每个人,甚至是匿名”,根据this answer。
相关问题
- 1. Google Apps脚本中的POST方法
- 2. 设置公共无效方法的JTextField
- 3. 公共设置方法不会更新
- 4. 为matplotlib直方图公开更新方法的简单方法
- 5. 公共方法
- 6. 在其他脚本中使用ASPX页面的公共方法
- 7. Google Apps脚本ToggleButton用法
- 8. 方法“moveActiveSheet”在Google Apps脚本(JavaScript)中无法正常工作
- 9. 使用Google Apps脚本获取Google Spreadsheet内容为JSON的简单方法是什么?
- 10. 简单的方法在Java脚本
- 11. Postgres脚本更简单的方法
- 12. Flex的 - 简单的方法来设置边界图像
- 13. 为ToolStripTextBox设置左侧文本边距的简单方法
- 14. 关于将检查所有公共方法的类的方面
- 15. 最简单的方法来隐藏类的一部分'公共方法/接口
- 16. 将值设置为wxTextCtrl的简单方法
- 17. 方法,在Google Apps脚本中用户登录的触发器
- 18. 调试Google Apps脚本Web应用程序的有效方法
- 19. 设置方法的返回值为公共字符串
- 20. 如何将包含的私有方法公开为公共类方法?
- 21. 相对于当前脚本导入共享python脚本的最简单方法
- 22. Ruby:在一组脚本中共享公共变量和方法
- 23. Google Apps脚本批量设置属性
- 24. 公共静态方法vs公共方法
- 25. 简单的Java方法将不会运行。里面方法
- 26. 使用Javascript或Google脚本将Google日历设置为公开
- 27. Google Apps脚本 - 使用.replace方法删除空格
- 28. 转换使用Google Apps脚本.toISOString()方法
- 29. 如何在Google Apps脚本中异步运行方法?
- 30. Google Apps脚本找不到方法isRequired(布尔值)