javascript 将数据发送到谷歌表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 将数据发送到谷歌表相关的知识,希望对你有一定的参考价值。

// Usage
// 1. Enter sheet name where data is to be written below
// 1. Enter sheet name and key where data is to be written below
var SHEET_NAME = "Hoja";
var SHEET_KEY = "SHEETKEY";

// 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) {
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(SHEET_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]);
  alert([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();
}
}

以上是关于javascript 将数据发送到谷歌表的主要内容,如果未能解决你的问题,请参考以下文章

将数据库中的坐标绘制到谷歌地图上,不断检查新值

如何将电子邮件发送到谷歌电子邮件? [复制]

谷歌应用脚​​本没有循环

是否可以将 javascript 插入谷歌广告?

JavaScript Javascript十进制到谷歌地图的度数

将远程 api json 数据显示到谷歌地图中