我将2列数据插入到Google电子表格中。我的表有3列A,B和C.列A,B数据通过表单插入,但列C数据通过formula = HTTPResponse()插入。当使用以下代码通过表单插入新数据时,如何将此公式复制到C列的新行?目前我的代码没有将代码复制到新行!
插入代码:
function doPost(e) {
var ss = SpreadsheetApp.openById(ScriptProperties.getProperty('active'));
var sheet = ss.getSheetByName("DATA");
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0]; //read headers
var nextRow = sheet.getLastRow(); // get next row
var cell = sheet.getRange('a1');
var col = 0;
for (i in headers){
if (headers[i] == "Timestamp"){
val = new Date();
} else if (headers[i] == "HTTPResponse"){ // (based on the assumption you add a column to you sheet with this name
val = '=HTTPResponse(B'+(nextRow+1)+')';
} else {
val = e.parameter[headers[i]];
}
cell.offset(nextRow, col).setValue(val);
col++;
}
var app = UiApp.createApplication();
var panel = app.createVerticalPanel();
for( p in e.parameters){
panel.add(app.createLabel(p +" "+e.parameters[p]));
}
app.add(panel);
return app;
}
function setUp() {
ScriptProperties.setProperty('active', SpreadsheetApp.getActiveSpreadsheet().getId());
}
//我想将下一个公式应用于新行:
unction HTTPResponse( uri )
{
var response_code ;
try {
response_code = UrlFetchApp .fetch( uri ) .getResponseCode() .toString() ;
}
catch( error ) {
response_code = error .toString() .match( / returned code (ddd)./ )[1] ;
}
finally {
return response_code ;
}
}
答案 0 :(得分:0)
代码的逻辑是在工作表标题中循环以匹配传递的参数。如果未找到列标题,则username
为val
。解决方案是为公式创建一个命名列,并创建另一个类似于timestamp的异常,例如:
undefined
如果由于性能原因,您希望在提交新值时运行 for (i in headers){
if (headers[i] == "Timestamp"){
val = new Date();
} else if (headers[i] == "HTTPResponse"){ // (based on the assumption you add a column to you sheet with this name
val = '=HTTPResponse(B'+(nextRow+1)+')';
} else {
val = e.parameter[headers[i]];
}
cell.offset(nextRow, col).setValue(val);
col++;
}
并返回静态值而不是将变量HTTPResponse()
设置为公式,则可以获得{{1}通过用以下代码替换行:
val
以下是a Google Sheet which implements both methods的副本(文件>制作副本以查看打开的工具>脚本编辑器)