简体   繁体   English

将不连续的选定行从电子表格复制到另一个

[英]Copy not contiguous selected rows from a spreadsheet to another

With this script is possible to copy the selected rows from a spreadsheet to another, from the column C to column E.使用此脚本可以将选定的行从电子表格复制到另一个电子表格,从 C 列到 E 列。
When the script is terminated, in the "source" spreadsheet, in column H appears the value sent for the rows copied.当脚本终止时,在“源”电子表格中,H 列中会出现为复制的行sent的值。

The problem is that the script take the rows selected in a contiguous range and so not with the possibility to copy not contiguous selected rows.问题是脚本采用在连续范围内选择的行,因此不可能复制不连续的选定行。

How can I solve this?我该如何解决这个问题?

function main() {

transfer("....", "Foglio1", "Foglio1");

}

function transfer(targetId, sourceSheetName, targetSheetName) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sourceSheet = ss.getSheetByName(sourceSheetName);
  var last = sourceSheet.getActiveRange().getRow();
  var height = sourceSheet.getActiveRange().getHeight();  
  var data = sourceSheet.getRange(last, 3, height, 3).getValues();

  // copy data
  var ss2 = SpreadsheetApp.openById(targetId);
  var targetSheet = ss2.getSheetByName(targetSheetName);
  //get last row
  var lastRow = targetSheet.getLastRow();
  //write data
  targetSheet.getRange(lastRow + 1, 3, data.length, data[0].length)
           .setValues(data);
  
  sourceSheet.getRange(last, 8, height).setValue('Sent');
}

I believe your goal as follows.我相信你的目标如下。

  • You want to copy the columns "C" to "E" of the selected rows in the source sheet to the destination sheet using Google Apps Script.您想使用 Google Apps 脚本将源工作表中选定行的“C”到“E”列复制到目标工作表。
  • When the values are copied, you want to put the value of "Sent" to the column "H" in the source sheet.复制值时,您希望将“已发送”的值放入源表中的“H”列。
  • In your case, the selected rows are not only the continuous rows, but also the discrete rows.在您的情况下,所选行不仅是连续行,而且是离散行。

Modification points:改装要点:

  • In this case, in order to retrieve the selected ranges, I would like to propose to use getActiveRangeList() and the method of spreadsheets.values.batchGet in Sheets API.在这种情况下,为了检索选定的范围,我想建议使用getActiveRangeList()和 Sheets API 中的电子表格电子表格getActiveRangeList()的方法。
    • I think that Spreadsheet service can also achieve your goal.我认为电子表格服务也可以实现您的目标。 But I thought that when Sheets API is used, the process cost will be low.但我认为当使用 Sheets API 时,处理成本会很低。 So in this answer, I would like to propose the method using Sheets API.所以在这个答案中,我想提出使用 Sheets API 的方法。
  • In order to put the value of "Sent", I used the range list.为了放置“已发送”的值,我使用了范围列表。

When above points are reflected to your script, it becomes as follows.当以上几点反映到你的脚本中时,它变成如下。

Modified script:修改后的脚本:

In this case, transfer() is modified.在这种情况下, transfer()被修改。 Before you use this script, please enable Sheets API at Advanced Google services .在使用此脚本之前, 请在高级 Google 服务中启用 Sheets API

function transfer(targetId, sourceSheetName, targetSheetName) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sourceSheet = ss.getSheetByName(sourceSheetName);
  
  // --- I modified below script.
  var ranges = sourceSheet.getActiveRangeList().getRanges().reduce((o, r) => {
    var row = r.getRow();
    var numRows = r.getNumRows();
    o.getValues.push(`${sourceSheetName}!C${row}:E${row + numRows - 1}`);
    o.setValue.push(`${sourceSheetName}!H${row}:H${row + numRows - 1}`);
    return o;
  }, {getValues: [], setValue: []});
  var data = Sheets.Spreadsheets.Values.batchGet(ss.getId(), {ranges: ranges.getValues}).valueRanges.reduce((ar, v) => ar.concat(v.values), []);
  
  data = data.map(r => !r ? Array(3).fill("") : (r.length < 3 ? r.concat(Array(3 - r.length).fill("")) : r));  // Added by OP's 3rd question in the comment.
  // ---
  
  // copy data
  var ss2 = SpreadsheetApp.openById(targetId);
  var targetSheet = ss2.getSheetByName(targetSheetName);
  //get last row
  var lastRow = targetSheet.getLastRow();
  //write data
  targetSheet.getRange(lastRow + 1, 1, data.length, data[0].length).setValues(data);  // Modified by OP's 2nd question in the comment.
  sourceSheet.getRangeList(ranges.setValue).setValue('Sent');  // Modified
}
  • In this case, both the continuous rows and the discrete rows can be used.在这种情况下,可以使用连续行和离散行。
  • When the discrete rows are selected, in this sample, the order of select is the order of the values.选择离散行时,在此示例中,选择的顺序是值的顺序。 Please be careful this.请注意这一点。

References:参考:

In addition to the elegant solution proposed by Tanaike, I would like to add for completion's sake a simple solution:除了 Tanaike 提出的优雅解决方案之外,为了完成起见,我想添加一个简单的解决方案:

  • You are defining var height = sourceSheet.getActiveRange().getHeight();您正在定义var height = sourceSheet.getActiveRange().getHeight();

  • The method getHeight() returns the height of an (adjacent) range. getHeight()方法返回(相邻)范围的高度。

  • If instead you want to copy all the range until the last data containing row - including empty rows, you can define instead:相反,如果您想复制所有范围,直到包含行的最后一个数据 - 包括空行,您可以改为定义:

    var height = sourceSheet.getActiveRange().getLastRow() - last + 1;

  • Now you will select and copy al the rows between the actively selected one and the last data containing row - including empty rows inbetween.现在,您将选择并复制主动选择的行和包含最后一个数据的行之间的所有行 - 包括中间的空行。

  • Mind that for large amounts of data, Tanaike's solution will be more efficient.请注意,对于大量数据,Tanaike 的解决方案会更高效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将电子表格中的多行复制到另一个电子表格 - Copy multiple rows from a spreadsheet to another spreadsheet 在Google Apps中从一个电子表格到另一个电子表格的非连续列复制 - Non-contiguous column copy from one spreadsheet to another in google apps 从电子表格复制到另一个具有动态标题的电子表格 - copy from spreadsheet to another spreadsheet with dynamic title 如何使用Google Apps脚本将特定的行从3个Google电子表格复制到另一个电子表格 - How to copy specific rows from 3 google spreadsheet to another spreadsheet using google apps script 将列值从一个电子表格复制到另一个电子表格,仅添加必要的行 - Copy column values from one spreadsheet to another spreadsheet, adding only the necessary rows 将范围从另一个电子表格复制到另一个 - Copy a range from another spreadsheet to another Google脚本:有条件地在同一电子表格中将行从一个工作表复制到另一个工作表 - Google Script: Conditionally copy rows from one sheet to another in the same spreadsheet 修改代码 - 将某些行/列从一个电子表格复制到另一个 - Google Apps Script / Google Sheets - Modify Code - Copy Certain Rows/Columns From one Spreadsheet to Another - Google Apps Script / Google Sheets 将谷歌脚本项目从电子表格复制到另一个 - Copy a google script project from a Spreadsheet to another 电子表格可复制其他电子表格中的信息-Javascript - Spreadsheet to copy informations from another sheets - Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM