简体   繁体   English

将电子表格中的多行复制到另一个电子表格

[英]Copy multiple rows from a spreadsheet to another spreadsheet

I'm writing a script that copy a selected row from a spreadsheet to another spreadsheet, from the column A to the column D.我正在编写一个脚本,将电子表格中的选定行复制到另一个电子表格,从 A 列到 D 列。
It works fine.它工作正常。
The problems are 3:问题是3:

  1. How to change the columns from A to D with the columns C and E.如何使用列 C 和 E 将列从 A 更改为 D。
  2. The script works only with one row selected, how can I copy multiple rows selected at the same time.该脚本仅适用于选定的一行,如何同时复制选定的多行。
  3. When the script is terminated, I would that in column H appears the value Sent for the rows copied.当脚本终止时,我会在 H 列中出现为复制的行Sent的值。

Thanks in advance.提前致谢。

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 data = sourceSheet.getRange(last, 1, 1, 4).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, 1, data.length, data[0].length)
           .setValues(data);
}

Explanation:解释:

  1. How to change the columns from A to D with the columns C and E?如何使用列 C 和 E 将列从 A 更改为 D?

Please have a look at the arguments of getRange :请看一下getRange的参数:

Replace代替

var data = sourceSheet.getRange(last, 1, 1, 4).getValues();

with

var data = sourceSheet.getRange(last, 3, 1, 3).getValues();


  1. The script works only with one row selected, how can I copy multiple rows selected at the same time?该脚本仅适用于选定的一行,如何同时复制选定的多行?

You can use getHeight to find the number of rows selected and pass it to getRange :您可以使用getHeight查找选定的行数并将其传递给getRange

var height = sourceSheet.getActiveRange().getHeight();  
var data = sourceSheet.getRange(last, 3, height, 3).getValues();

  1. When the script is terminated, in column H appears the value "Sent" for the rows copied.当脚本终止时,在 H 列中会出现复制行的值“已发送”。

You can just set the values of column H to Sent for the selected rows:您只需将 H 列的值设置为所选行的已Sent

sourceSheet.getRange(last, 8, height).setValue('Sent');


Solution:解决方案:

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');
}

暂无
暂无

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

相关问题 将不连续的选定行从电子表格复制到另一个 - Copy not contiguous selected rows from a spreadsheet to another 从电子表格复制到另一个具有动态标题的电子表格 - 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 应用程序脚本将多个范围从一张工作表复制到另一个电子表格 - Apps script copy multiple range from 1 sheet to another spreadsheet 仅将值从多个选项卡复制到另一个电子表格的脚本 - Script to copy values only from multiple tabs to another spreadsheet 从一个电子表格更新到另一电子表格时追加多个行 - Appending Multiple Rows while updating from one spreadsheet to another 将多个工作表从一个电子表格复制到另一个电子表格 - Copy several sheets from one spreadsheet to another spreadsheet 将动态范围从一个电子表格复制到另一个使用getfileid调用的电子表格 - copy dynamic range from one spreadsheet to another spreadsheet called with getfileid 如何将脚本从一个电子表格复制到另一电子表格? - How do I copy a script from one spreadsheet to another spreadsheet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM