【问题标题】:Posting to Google Sheet is giving {"result":"error","error":{"name":"Exception"}}发布到 Google 表格会给出 {"result":"error","error":{"name":"Exception"}}
【发布时间】:2021-10-07 01:51:12
【问题描述】:

我正在尝试使用 html 表单发布到谷歌表格,我按照教程 here,但我不断收到错误 {"result":"error","error":{"name":"Exception"}} 并且表格没有更新。

这里是code.gs

// original from: http://mashe.hawksey.info/2014/07/google-sheets-as-a-database-insert-with-apps-script-using-postget-methods-with-ajax-example/
// original gist: https://gist.github.com/willpatera/ee41ae374d3c9839c2d6 
/**
 * @OnlyCurrentDoc
 */
function doGet(e){
  console.log("running");
  return handleResponse(e);
}

// Usage
//  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 doPost(e){
  console.log("running2");
  return handleResponse(e);
}
function handleResponse(e) {
  console.log("running3");
  // 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
    console.log("running4");
    var doc = SpreadsheetApp.openById(SCRIPT_PROP.getProperty("key"));
    console.log("running5");
    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]]);
        //row.push("Test")
      }
    }
    // 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());
}

我输入了 console.logs 以查看问题出在哪里,它似乎与 var doc = SpreadsheetApp.openById(SCRIPT_PROP.getProperty("key")); 行有关,因为记录了“running4”,但没有记录“running5”。

【问题讨论】:

    标签: google-apps-script google-sheets


    【解决方案1】:

    发现是权限错误

    /**
     * @OnlyCurrentDoc
     */
    

    在脚本中。我添加了这一点,因为我在尝试授予我的脚本访问权限时不断收到安全错误,但我通过 this answer 解决了这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-27
      • 2014-05-10
      • 2013-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多