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

查看:17
本文介绍了用于在 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 表格中的一个按钮.数据从 Report! 发送到 Records!.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天全站免登陆