简体   繁体   English

将特定范围复制到从一个工作表到另一个工作表中的第一个空行和特定列

[英]Copy a specific range to from one sheet to the first empty row and specific column in another sheet

I use Google Sheets and I'm trying to use a script that copies certain ranges from a sheet (that reads CSV files) to a separate sheet's last available row, but to a certain column.我使用 Google 表格,我正在尝试使用一个脚本,该脚本将某些范围从表格(读取 CSV 文件)复制到单独的表格的最后一个可用行,但复制到某个列。 I also don't want to copy empty values.我也不想复制空值。

https://i.stack.imgur.com/lgGP3.png https://i.stack.imgur.com/lgGP3.png

Image 2图 2

So I need to copy values only of a specific range from a sheet, like Sheet1:A5,D14, where it ignores empty values, to a different sheet's first empty row.因此,我只需要将特定范围的值从工作表(如 Sheet1:A5,D14,它忽略空值)复制到不同工作表的第一个空行。 but not the same column (so everything moves forward to column CF).但不是同一列(因此所有内容都移至 CF 列)。

I was able to use this, to copy things to a specific column:我能够使用它来将内容复制到特定列:

function copyRange(e){
  var copyFromRange = 'Sheet1!B3:J10'; // no row for second cell reference
  var copyToRangeStart = 'Sheet2!B12';
  copyValuesOnly(copyFromRange, copyToRangeStart);
}

function copyValuesOnly(copyFromRange, copyToRangeStart) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var source = ss.getRange(copyFromRange);
  source.copyTo(ss.getRange(copyToRangeStart), {contentsOnly: true});
}

However, this copies empty rows too, and can't find the first empty row in the other sheet.但是,这也会复制空行,并且在另一张工作表中找不到第一个空行。

I tried using code like this;我尝试使用这样的代码;

function moveValuesOnly() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var source = ss.getRange("B10:AH13");
  var destSheet = ss.getSheetByName("Test");
  destSheet.appendRow(source.getValues()[0]);
}

This only copies a single row, and I can't copy it to a certain column on the other sheet, will always start in "A".这只会复制一行,我不能将它复制到另一张纸上的某一列,总是从“A”开始。

I'm not big on coding, if someone could help me here, that'd be amazing.我不擅长编码,如果有人可以在这里帮助我,那就太棒了。

In short, the function I need has to;简而言之,我需要的 function 必须;

  • copy a selected range of values without formatting复制选定范围的值而不进行格式化
  • not copy empty rows不复制空行
  • paste to the first empty available row in a different sheet粘贴到不同工作表中的第一个可用空行
  • adjust which column it starts pasting the copied data to.调整它开始将复制的数据粘贴到哪一列。

EDIT:编辑:

  • keep previous data in sheet 2 (new data doesn't overwrite anything)将以前的数据保留在工作表 2 中(新数据不会覆盖任何内容)
  • function repeatable infinitely, the new data always gets added in the empty rows below in sheet 2. function 可无限重复,新数据总是添加到工作表 2 下方的空行中。

Thanks to anyone who takes the time to help in advance:)感谢任何花时间提前帮助的人:)

function copyRange(){
  const ss=SpreadsheetApp.getActive();
  const srcrg = 'Sheet1!B3:J10'; 
  const torg = 'Sheet2!B12';
  const desrg=ss.getRange(torg);
  const row=desrg.getLastRow();
  const col=desrg.getColumn();
  const dessh=desrg.getSheet();
  const src=ss.getRange(srcrg).getValues();
  let cnt=0
  src.forEach((r,i)=>{
    if(r.join("")!="") {
      dessh.getRange(row+1+cnt++,col,1,r.length).setValues([r]);
    }
  });
}

Source:资源:

COL1 COL1 COL2 COL2 COL3 COL3 COL4 COL4 COL5 COL5 COL6 COL6 COL7 COL7 COL8 COL8 COL9 COL9 COL10 COL10
10 10 21 21 14 14 11 11 6 6 9 9 3 3 11 11 9 9 2 2
29 29 29 29 21 21 19 19 14 14 4 4 24 24 11 11 1 1 0 0
2 2 11 11 25 25 2 2 9 9 2 2 1 1 11 11 5 5 29 29
3 3 3 3 24 24 18 18 28 28 6 6 6 6 20 20 19 19 23 23
4 4 16 16 4 4 17 17 25 25 14 14 8 8 22 22 4 4 15 15
26 26 15 15 7 7 25 25 7 7 2 2 10 10 26 26 10 10 19 19
4 4 18 18 6 6 5 5 18 18 27 27 2 2 27 27 5 5 0 0
15 15 5 5 26 26 22 22 13 13 18 18 6 6 27 27 28 28 24 24

Destination:目的地:

29 29 21 21 19 19 14 14 4 4 24 24 11 11 1 1 0 0
11 11 25 25 2 2 9 9 2 2 1 1 11 11 5 5 29 29
3 3 24 24 18 18 28 28 6 6 6 6 20 20 19 19 23 23
16 16 4 4 17 17 25 25 14 14 8 8 22 22 4 4 15 15
15 15 7 7 25 25 7 7 2 2 10 10 26 26 10 10 19 19
18 18 6 6 5 5 18 18 27 27 2 2 27 27 5 5 0 0
5 5 26 26 22 22 13 13 18 18 6 6 27 27 28 28 24 24

暂无
暂无

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

相关问题 如何将范围从一个工作表复制到另一工作表的范围,然后重复复制到下一行 - How to copy a range from one sheet to a range in another sheet, then repeat copying to the next row down 谷歌脚本将数据从工作表 1 复制到第一个空行工作表 2 - google script copy data from sheet 1 to first empty row sheet 2 如何将特定数据按特定顺序从一张纸复制到另一张纸 - how can copy specific data from one sheet to another sheet in specific order 根据特定的键值将数据从一张纸复制到另一张纸 - Copy data from one sheet to another based on a specific key value 将范围从一个工作表复制到Google工作表中的另一个工作表 - Copy a range from one sheet to another in google sheet 将原始数据从工作表“源”复制到另一个工作表“目的地”,并在Google电子表格行的第一列中添加日期 - Copy a raw from sheet 'source' to another sheet 'destination' adding the date in the first column of the row Google spreadsheet 如何将值从一张纸粘贴到另一张纸到特定列的最后一行 - How to paste values from one sheet to another to last row of specific column Google Spreadsheets 脚本 - 如何将范围从一张纸复制到另一张纸,但不覆盖非空目标单元格 - Google Spreadsheets script - How to copy range from one sheet to another sheet, but without overwriting non-empty target cells 如何使用 Google Sheet Script 将数据范围从一个工作表复制到另一个工作表 - How to Copy a data range from one sheet to another sheet using Google Sheet Script 我正在编写一个脚本,以便在表 2 的 A 列中输入特定的客户代码时,它将匹配并复制表 1 中的匹配行 - I'm writing a script so that on entering a specific customer code in column A in Sheet 2 it will match and copy the matching row from Sheet 1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM