简体   繁体   English

将值从 ajax 传递到 Google Apps 脚本

[英]Pass value from ajax to Google Apps Script

I'm new to Google Apps Script and I'm trying to make a web app where I can pass the value from Ajax to my script's web app.我是 Google Apps Script 的新手,我正在尝试制作一个网络应用程序,我可以在其中将值从 Ajax 传递到我脚本的网络应用程序。 I'm able to execute it directly from the web app but it's not working from my webpage.我可以直接从网络应用程序执行它,但它不能从我的网页上运行。 Here are my codes,这是我的代码,

Webpage's JS网页的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!");
      }
    });
  });
});

Google Apps Script Google Apps 脚本

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"]]);
}

How can I pass the value from ajax to my apps script?如何将值从 ajax 传递到我的应用程序脚本?

Try appending the data you're sending out onto the end of the url:尝试将您发送的数据附加到 url 的末尾:

"ScriptUrl/exec?getIdKey=getIdValue"

Docs for that.文档

Your data property is likely just being ignored.您的data属性可能只是被忽略了。

The value can then be accessed in doGet(e) with:然后可以在doGet(e)访问该值:

var getIdValue = e.parameter.getIdKey

Docs for that 文档

You will have to make a page redirection and navigate to the script's published url on a click or something您必须进行页面重定向并通过单击或其他方式导航到脚本的已发布网址

because XHR requests to the Google Apps Script server are forbidden因为禁止向 Google Apps 脚本服务器发送 XHR 请求

From an external client XHR requests are forbidden but within the Google App Script editor calls to server side scripts(ie any one of the functions in .gs file) are by default asynchronous and can be made using google.script.run来自外部客户端的 XHR 请求被禁止,但在 Google App Script 编辑器中对服务器端脚本(即 .gs 文件中的任何一个函数)的调用默认是异步的,可以使用google.script.run

For more please refer to the official documentation.更多请参考官方文档。

Google App Script Restrictions Google App 脚本限制

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将值从 HTML 传递到 Google Apps 脚本上的 Google 脚本? - Pass value from HTML to Google Script on Google Apps Script? Google Apps脚本:将返回值从一个函数传递给另一个函数 - Google Apps Script: pass return value from one function to another 无法将 InputBox 字符串值从 HTML 传递到 Google Apps 脚本 - Cannot Pass InputBox String value From HTML To Google Apps Script 如何将HTML复选框的值传递给Google Apps脚本? - How do I pass the value of a HTML checkbox to Google Apps Script? 如何将值从HTML表单提交传递给Google表格,然后再传递回Google Apps Script Web App中的HTML - How do I pass a value from an HTML form submission to a Google Sheet and back to HTML in a Google Apps Script Web App 将 Google Apps 脚本变量传递给 HTML 脚本 - Pass Google Apps Script variable to HTML script 如何将元素值变量从 Javascript html 文件传递到 google apps 脚本中的 code.gs 文件? - How to pass an element-value variable from Javascript html file to code.gs file in google apps script? Google Apps脚本错误值 - Google Apps Script Bad Value 使用 Google Apps 脚本从数组中获取最后一个值 - Get last value from an array using Google Apps Script Google Sheets Apps 脚本——来自多行单元格的 select 值 - Google Sheets Apps Script -- select value from cell with multiple lines
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM