我使用此解决方案将在线调查的条目发布到Google电子表格中:
https://gist.github.com/hamx0r/b851531d8546565c23deab926ee6867e
/*
Copyright 2011 Martin Hawksey
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// 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 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(headRow, 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());
}
除非用户省略填写调查页面上的字段,否则相应的单元格在Google表格中显示为空白。这是非常正常和直观的,但是对于我之后要做的事情,我需要空白单元格而不是字符串" ..."。
所以我写了这篇文章(在另一个答案的帮助下):
function fillBlanks() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("A:W");
var last_row=sheet.getLastRow();
var data=sheet.getRange(1,1,last_row,23).getValues();
for (var i = 1; i < last_row + 1; i++) {
for(var y = 1; y < 23 ; y++){
var cell = range.getCell(i, y);
if (cell.isBlank()) {
cell.setValue("...");
}
}
}
}
此功能在另一个文件中,如下:
现在,每次第一个脚本从调查页面获取Post并插入一行时,我都需要调用fillBlanks()。也就是说,我需要在完成更改后调用此函数。而且我找不到合适的方法。
我尝试插入&#34; fillBlanks();&#34;在handleResponse函数结束时,没有成功...
我应该在何处以及如何调用fillBlanks()来实现这一目标?
答案 0 :(得分:1)
在您的脚本中,当doGet()
运行时,您似乎将e.parameter
的密钥headers[i]
放在row.push(e.parameter[headers[i]]);
。此时,如果您不希望在将值赋予doGet(e)
时将空白,那么此修改如何?
row.push(e.parameter[headers[i]]);
var v = e.parameter[headers[i]];
row.push(v || "...");
e.parameter[headers[i]]
,则会放置...
。fillBlanks()
,当Web Apps使用电子表格时,需要直接使用文件ID。因此,需要将其从var ss = SpreadsheetApp.getActiveSpreadsheet();
修改为var ss = SpreadsheetApp.openById("### fileId ###");
。如果我的理解不正确,我很抱歉。