将值从 ajax 传递到 Google Apps Script
Pass value from ajax to Google Apps Script
我是Google Apps Script的新手,我正在尝试制作一个Web应用程序,我可以在其中将值从Ajax传递到脚本的Web应用程序。我可以直接从 Web 应用程序执行它,但它无法从我的网页上工作。这是我的代码,
网页的 JS
$(document).ready(function () {
$('#btnGet').click(function () {
var getId = $('#text').val();
$.ajax({
url: "https://script.google.com/macros/s/AKfycbwpkBW7qKZBeJ011C7j3le4vV8D0SEHu9709mWtEMzJgrJmHnaR/exec",
data: getId,
type: "GET",
crossDomain: true,
success: function (data) {
console.log(data);
alert("Success!");
},
error: function (data) {
console.log(data);
alert("Failed!");
}
});
});
});
谷歌应用套件
function doGet(e) {
var html ="<input type='text' id='text' /><br><br><input type='button' id='btnGet' onclick='myClick()' value='submit' /><br><br>";
html+="<script>";
html+="document.getElementById('btnGet').onclick = function() { var e = document.getElementById('text').value; google.script.run.newEntry(e); }</script>";
return HtmlService.createHtmlOutput(html);
}
function newEntry(text) {
Logger.log(text);
var ss = SpreadsheetApp.openById(text);
var sheet = ss.getSheetByName("Sheet1");
var lr = sheet.getLastRow();
sheet.getRange(lr+1, 1, 1, 3).setValues([["First Name", "Last Name", "Email"]]);
}
如何将值从 ajax 传递到我的应用程序脚本?
尝试将
您发送的数据附加到 url 的末尾:
"ScriptUrl/exec?getIdKey=getIdValue"
文档。
您的data
财产可能只是被忽略了。
然后,可以使用以下命令doGet(e)
访问该值:
var getIdValue = e.parameter.getIdKey
文档
您必须进行页面重定向,并在单击或其他内容时导航到脚本的已发布网址
因为禁止对 Google Apps 脚本服务器的 XHR 请求
禁止从外部客户端发出XHR请求,但在Google App Script编辑器中,对服务器端脚本(即.gs文件中的任何一个函数)的调用默认是异步的,可以使用google.script.run
有关更多信息,请参阅官方文档。
谷歌应用脚本限制
相关文章:
- Google Apps Script中的标准Javascript
- Google Apps 脚本为 getLastRow 抛出电子邮件失败通知,我做错了什么
- Google Apps 脚本在单元格中返回行值
- 在Google Apps上从JavaScript迁移到Python
- 在 Google Apps 脚本中,无法以编程方式创建触发器以从 onEdit 触发器发送电子邮件
- Google Apps 脚本中的格式化日期
- Google Apps 脚本:尝试根据单元格的背景颜色发送电子邮件
- Google Apps 脚本,用于根据一个单元格的值对另一个单元格进行条件颜色格式设置
- 如何转换SalesForce Api提供的UTC日期格式,并使用Google Apps脚本将其转换为本地日期和时间格式
- Google UiApp中的Google Apps脚本验证问题
- 添加“;密码”;键入Google Apps Script inputBox
- 找不到Google Apps脚本库嵌套函数
- HTTP POST 和 Google Apps Script 文件上传
- Google Apps Script 中基于 Twitter OAuth Pin 的授权“oob”
- Google-apps-script不会在onEdit函数中发送电子邮件
- 在 Google Apps Script Web App 中显示文件和文件夹
- 使用 Google Apps 脚本处理数组中输入元素中的多个文件
- 在 Google Apps 脚本中调用函数
- Google 电子表格:如何使用 Google Apps 脚本命名范围
- Google Apps 脚本:解析 XML 会导致错误:“无法在对象中找到函数 getChildren”