【问题标题】:Google apps script(Error script function not found doPost )Google 应用程序脚本(找不到错误脚本函数 doPost )
【发布时间】:2019-04-28 17:46:17
【问题描述】:

Error image on Browser

这是我的谷歌网络应用脚本中的代码,doPost 已定义,但我不断收到同样的错误,我的谷歌表格没有注册用户的输入,它是“未定义”

 // original from: http://mashe.hawksey.info/2014/07/google-sheets-as-a-database-insert-with-apps-script-using-postget-methods-with-ajax-example/

function doGet(e){
  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){
  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("Success")
          .setMimeType(ContentService.MimeType.TEXT);
  } catch(e){
    // if error return this
    return ContentService
          .createTextOutput("Failure")
          .setMimeType(ContentService.MimeType.TEXT);
  } finally { //release lock
    lock.releaseLock();
  }
}

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

; }

Google sheets

这是另一个我不明白的错误。在我的 html 代码中,我已经完全按照我在工作表中创建的每个单元格上显示的方式定义了我的用户输入。

【问题讨论】:

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


    【解决方案1】:

    您可以参考这个thread。保存和重新发布您的 google script web 应用程序可能是问题所在。您需要保存新版本并发布新版本,以确保您的应用得到正确更新。

    也来自这个blog

    如果您发送 POST 请求但没有 POST 请求处理程序,也会出现同样的问题。然后你会看到错误,“Script function not found: doPost”错误。

    【讨论】:

      【解决方案2】:

      如果您在请求正文中使用 JSON 进行 POST,如果您能够找到指向 original code 的作者的链接,他会稍微更新一下。

      var jsonString = e.postData.getDataAsString();
      e.parameter = JSON.parse(jsonString);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-27
        • 1970-01-01
        • 1970-01-01
        • 2023-03-31
        相关资源
        最近更新 更多