【问题标题】:How to apply functions/formulas from last row after inserting a new row to google sheet?将新行插入谷歌表后如何应用最后一行的函数/公式?
【发布时间】:2015-10-04 20:51:11
【问题描述】:

我将 2 列数据插入到谷歌电子表格中。我的工作表有 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 ;
}
}

【问题讨论】:

  • 你试过用这一行“cell.offset(nextRow, col).setValue('=HTTPResponse()');”由于变量 'col' 增加了,我认为您可以在 for 循环之后添加该行,它将在 C 列中添加公式。检查它是否有效。
  • 感谢您的回复。我在 cell.offset(nextRow, col).setValue(val); 之后添加了您的行我在 c 列中得到了“未定义”的值!我该如何解决?
  • 您是否还添加了网址? HTTPResponse 函数需要一个 url,因此它可以使用 URLFecht.fetch 函数调用它。
  • 我有一个表单,我总是传递给表单一个 url 和 name 。 url 和名称已成功插入工作表,但在 C 列而不是服务器状态上,我得到“未定义”值。我想我也应该通过考试 =HTTPResponse(B4) 的列 ID。如何传递每个单元格 id ?
  • 我什至在插入到行之前输入了列 id 但没有用(cell.offset(nextRow, col).setValue('=HTTPResponse(B4)');)

标签: google-apps-script google-sheets spreadsheet


【解决方案1】:

代码的逻辑是循环遍历工作表标题以匹配传递的参数。如果未找到列标题,则 valundefined。解决方案是为公式创建一个命名列并创建另一个类似于时间戳的异常,例如:

  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() 并返回静态值,而不是将变量 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 的副本(文件> 制作副本以查看打开的工具> 脚本编辑器)

【讨论】:

  • 感谢您的回复。我按照您的建议将 C 列命名为 HTTPResponse,并使用您发布的代码,当数据插入行时,我仍然在新行(C 列)中得到 udefined!是否可以参考 url 的值(在 google 脚本内)不是来自工作表单元格而是来自 post 方法并运行 httresponse 并将其值放置到 c 列?有没有其他方法可以解决这个问题?
  • @user1788736 我已经更新了答案,包括如何将静态值放入单元格以及实现这两种方法的示例 Google 表格(请记住文件>复制以查看工具>脚本编辑器代码)
  • @user1788736 .. 还请记住,当您更新代码时,您需要发布新版本,否则应用程序不会使用您的编辑,除非处于开发模式
  • 感谢更新代码。我尝试了你的脚本它工作但在列 C 上添加额外的 url 名称值列后,现在我在 E 上未定义!我应该在代码的哪里定义列数? A(timestamp),B(url),c(urlname),d(httpresponse formula) and E(httresponse static)
  • 您确定要在帖子有效负载中传递 urlname 值吗?它将区分大小写和空格。 hurl.it 是我用来测试的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-24
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
  • 1970-01-01
相关资源
最近更新 更多