在Google表格中追加数据范围的脚本 [英] Script to append range of data in Google Sheets

查看:315
本文介绍了在Google表格中追加数据范围的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本可以复制并发送一系列数据到另一个表单。我想添加另一部分到脚本,它将执行相同的功能,但将数据追加到新行。

I have a script that will copy and send a range of data to another sheet. I want to add another part to the script, that will do the same function but append the data to a new row.

  function saveToRecords() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var source = ss.getRange('Report!L1:AC1');
    source.copyTo(ss.getRange('Records!A3:R3'), {contentsOnly: true});
  }

我已将此脚本附加到Google表格中的按钮上。数据从报告!发送到记录! Records!工作表会保存这些数据。所以每次我点击按钮,我都希望数据被发送到一个新的行。

I have this script attached to a button in Google Sheets. The data is sent form Report! to Records!. The Records! sheet will save this data. So each time I click the button I would like the data to be sent to a new row.

我知道有一个简单的方法可以做到这一点!我一直难住。谢谢你的帮助。

I know there is an easy way to do this! I have been stumped. Thanks for your help.

推荐答案

是的,有。您可以使用 getLastRow()

yes, there is. You can use getLastRow().

所以你的代码如下所示:

So your code would look like this:

function appendToRecords() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var reportSheet = ss.getSheetByName("Report");
  var recordsSheet = ss.getSheetByName("Records")
  var reportData = reportSheet.getRange("L1:AC1")
                              .getValues();
  var lastRow = recordsSheet.getLastRow();
  //copy data
  recordsSheet.getRange(lastRow + 1, 12, 1, 18)
              .setValues(reportData);

}

这篇关于在Google表格中追加数据范围的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆