javascript - 在 Javascript 中调用 GAS 的 doPost 函数
问题描述
我需要doPost
从我的 Javascript 代码中调用 Google Apps 脚本端的一个函数。
我已经使用 a 完成了它,doGet
但是我需要传递很多变量,所以 adoGet
不是最好的。
这是我用于 的代码doGet
,它适用于具有少量输入的表单,但我需要以更大的形式实现逻辑。
我必须从按钮调用函数
<input type="button" onclick="get()" value="Go" class="btn">
JS:
function get() {
var SCRIPT_URL = "https://script.google.com/macros/s/key/exec";
$.getJSON(SCRIPT_URL + "?a="+ $("#a").val(),
{ method: "populate_list" },
function (data) {
alert(JSON.stringify(data));
});
};
在 Google Apps 脚本中,这是我拥有的 doPost 函数
function doPost(e) {
if (e.parameter.method=="populate_list") {
SpreadsheetApp.getActiveSheet().getRange('F2').setValue(e.parameter.a);
}
}
我需要的是按钮调用的函数 get() 但对于 doPost 的等价物,因此它调用我在 GAS 中的 doPost 函数
解决方案
Jquery $.post() 适用于此,我从表单中获取值并将其传递给脚本 url。
JS:
var v_name = $("#name").val();
$.post("https://script.google.com/macros/s/KEY/exec",
{
index: selectedUser,
name: v_name,
variableName: variableValue
},
function (data) {
console.log(data.name);
}, 'json');
谷歌脚本
function doPost(e) {
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange('B'+e.parameter.index + "").setValue(e.parameter.name);
}
推荐阅读
- google-cloud-firestore - Firestore:一个原子操作中的事务和批处理
- c++ - 返回不可复制对象的元组
- vue.js - Vue.js中如何根据数据属性自动构造watch属性?
- node.js - 尝试连接reactjs和域名时主机标头无效
- python - REST API 发送的数据不适合 Web 服务的格式(逻辑回归模型)
- sql-server - 带有 VS Code(和 Azure Data Studio)中的 SQL Server 扩展的 Ubuntu 上的传输级别错误
- kofax - 检测 Kofax 是否启动了自定义模块或用户
- javascript - 如何使用 Webpack 导出函数并在 HTML 页面中使用?
- javascript - 为什么 req.body 在我的显示和编辑路线中未定义,而不是我的创建路线?
- php - WooCommerce:如果购物车中有特定的产品 ID,则显示自定义结帐字段