如何在向 google sheet 插入新行后从最后一行应用 functions/formulas?
How to apply functions/formulas from last row after inserting a new row to google sheet?
我正在将 2 列数据插入到 google spreadsheet 中。我的 sheet 有 3 列 A,B 和 C。 A、B 列数据通过表单插入,但 C 列数据通过公式 =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 ;
}
}
代码的逻辑是循环遍历 sheet 标题以匹配传递的参数。如果未找到列标题,则 val
为 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()
和 return 静态值而不是将变量 val
设置为一个公式,您可以通过将行替换为:
来获得 HTTPResponse()
值
// assuming that url is the name of the POST value with the url
val = HTTPResponse(e.parameter.url);
这是a Google Sheet which implements both methods的副本(文件>制作副本以查看打开的工具>脚本编辑器)
我正在将 2 列数据插入到 google spreadsheet 中。我的 sheet 有 3 列 A,B 和 C。 A、B 列数据通过表单插入,但 C 列数据通过公式 =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 ;
}
}
代码的逻辑是循环遍历 sheet 标题以匹配传递的参数。如果未找到列标题,则 val
为 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()
和 return 静态值而不是将变量 val
设置为一个公式,您可以通过将行替换为:
HTTPResponse()
值
// assuming that url is the name of the POST value with the url
val = HTTPResponse(e.parameter.url);
这是a Google Sheet which implements both methods的副本(文件>制作副本以查看打开的工具>脚本编辑器)