我正在尝试在作为 WebApp 运行的 Google App 脚本中使用 doGet() 返回 Google 电子表格的标题行。我正在使用 HTML 表单将 GET 请求发送到 WebApp,一切正常,只是我不知道如何将 header 返回到我的 javascript。我将发布我的代码:
HTML:
<form id="getForm" method="get" action="My URL for WebApp">
<label for="sheetGetID">SheetID</label>
<input type="text" name="sheetGetID" id="sheetGetID" value="">
<button class="ui-btn" onclick='submitGET()'>Submit</button>
</form>
Javascript:
function submitGET() {
var headers = $("getForm").submit();
alert(headers);
}
Google 应用脚本:
function doGet(e) {
//Trying To: Get headers from sheetID and then return to app, then have correct labels for the inputs, then use POST to post.
var ss = SpreadsheetApp.openById(ScriptProperties.getProperty('active'));
var sheet = ss.getSheetByName(e.parameter["sheetGetID"]);
//Return the first 3 cells, A1:C1,
var headers = sheet.getRange(1,1,1,sheet.getLastColumn()).getValues()[0];
return ContentService.createTextOutput(JSON.stringify(headers))
.setMimeType(ContentService.MimeType.JSON);
}
我返回了一个 JSON 对象,但它只是一个文本输出。我的问题是如何获取 JSON 返回并存储为 headers 变量?
最佳答案
doGet方法的返回值必须是HTML。 构建另一个 html 页面并使用调用 HtmlService。 createTemplateFromFile ('newPag.html').evaluate()
在页面内使用标签并放置操作 json 对象的服务器端代码。这样您将创建良好的外观和良好的可维护代码。
关于javascript - 使用 HTTP GET 和 Google App Script 访问 Google 电子表格的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20873192/