【问题标题】:Using Google sheet script to push data from HTML form to sheet not working when sheet is not open当工作表未打开时,使用 Google 工作表脚本将数据从 HTML 表单推送到工作表不起作用
【发布时间】:2017-10-26 19:04:43
【问题描述】:

我正在使用 Google App 脚本将数据从 HTML 表单发送到 Google 电子表格。当我在浏览器中打开电子表格时,这非常有效;我在表格中输入的数据被提交到工作表。另一方面,当我关闭电子表格并填写表格时,注释会提交到工作表中。

Here is my script.

//  1. Enter sheet name where data is to be written below
        var SHEET_NAME = "Sheet1";

//  2. Run > setup
//
//  3. Publish > Deploy as web app
//    - enter Project Version name and click 'Save New Version'
//    - set security level and enable service (most likely execute as 'me' and access 'anyone, even anonymously)
//
//  4. Copy the 'Current web app URL' and post this in your form/script action
//
//  5. Insert column names on your destination sheet matching the parameter names of the data you are passing in (exactly matching case)

var SCRIPT_PROP = PropertiesService.getScriptProperties(); // new property service

// If you don't want to expose either GET or POST methods you can comment out the appropriate function
function doGet(e){
  return handleResponse(e);
}

function doPost(e){
  return handleResponse(e);
}

function handleResponse(e) {
  // shortly after my original solution Google announced the LockService[1]
  // this prevents concurrent access overwritting data
  // [1] http://googleappsdeveloper.blogspot.co.uk/2011/10/concurrency-and-google-apps-script.html
  // we want a public lock, one that locks for all invocations
  var lock = LockService.getPublicLock();
  lock.waitLock(30000);  // wait 30 seconds before conceding defeat.

  try {
    // next set where we write the data - you could write to multiple/alternate destinations
    var doc = SpreadsheetApp.openById(SCRIPT_PROP.getProperty("key"));
    var sheet = doc.getSheetByName(SHEET_NAME);

    // we'll assume header is in row 1 but you can override with header_row in GET/POST data
    var headRow = e.parameter.header_row || 1;
    var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
    var nextRow = sheet.getLastRow()+1; // get next row
    var row = [];
    // loop through the header columns
    for (i in headers){
      if (headers[i] == "Timestamp"){ // special case if you include a 'Timestamp' column
        row.push(new Date());
      } else { // else use header name to get data
        row.push(e.parameter[headers[i]]);
      }
    }
    // more efficient to set values as [][] array than individually
    sheet.getRange(nextRow, 1, 1, row.length).setValues([row]);
    // return json success results
    return ContentService
          .createTextOutput(JSON.stringify({"result":"success", "row": nextRow}))
          .setMimeType(ContentService.MimeType.JSON);
  } catch(e){
    // if error return this
    return ContentService
          .createTextOutput(JSON.stringify({"result":"error", "error": e}))
          .setMimeType(ContentService.MimeType.JSON);
  } finally { //release lock
    lock.releaseLock();
  }
}

function setup() {
    var doc = SpreadsheetApp.getActiveSpreadsheet();
    SCRIPT_PROP.setProperty("key", doc.getId());
}

【问题讨论】:

  • 您的脚本是独立脚本还是绑定到电子表格?请注意,由于共享包含完整代码的文件可能很有用,因此问题应该是自包含的,因此建议在问题中包含 minimal reproducible example
  • @Rubén 脚本已绑定到电子表格。正如我所说,当我打开工作表时,数据会提交到工作表,但当我没有打开工作表时不会进入工作表。如果我也附上我的 html 代码会有帮助吗?
  • 我建议您将示例代码嵌入到此问题中,以便在推荐解决方案时,该页面将包含整个问答。
  • @terrywb 我会这样做的,谢谢!

标签: javascript html google-apps-script google-sheets


【解决方案1】:

我怀疑当您关闭电子表格时没有“活动电子表格”。

function setup() {
    var doc = SpreadsheetApp.getActiveSpreadsheet();
    SCRIPT_PROP.setProperty("key", doc.getId());
}

我建议您将电子表格 ID 硬编码到您的代码中。

【讨论】:

  • 我该怎么做?抱歉,我是 Google 脚本的新手。
  • 当您的电子表格打开时,doc.getId() 返回的值也会显示在 URL 中。只需将 doc.getId() 替换为该值即可。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-12
  • 1970-01-01
  • 1970-01-01
  • 2019-12-19
  • 1970-01-01
  • 2022-10-14
相关资源
最近更新 更多