简体   繁体   English

通过for循环谷歌表格脚本将行复制到另一张表格

[英]Copy row to another sheet via for loop google sheets script

I am below a newbie and have tried this code several ways and I just can't seem to figure out what is going on.我是新手,已经尝试了几种方法,但我似乎无法弄清楚发生了什么。

What I am trying to do is copy any new lines that get added to one sheet ( Source ) to another sheet ( Target ).我想要做的是将添加到一张纸( Source )的任何新行复制到另一张纸( Target )。 There is a column (7) that is there to indicate not to copy that row from the Source to the Target , and is set to CP when the program runs through and copies to the Target .有一列 (7) 指示不要将该行从Source复制到Target ,并且在程序运行并复制到Target时设置为CP

The code below seems to work and copy to the Target with the exception that it copies every row from the Source every time I run it, even when the Source rows have been set to CP in col 7 ( CP means copied if you are wondering).下面的代码似乎可以工作并复制到Target ,但每次我运行它时它都会从Source复制每一行,即使在第 7 列中将源行设置为CP (如果您想知道, CP意味着复制) .

Any help would be appreciated in helping me figure this out.如果能帮助我解决这个问题,我们将不胜感激。 I've banged my head against the desk a few times already but that does not seem to help either.我已经用头撞过桌子好几次了,但这似乎也无济于事。

function copyRowToTarget() {

  var sourceSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Source')
  var targetSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Target') 
  var startRow = 2; 
  var sSLastRow = sourceSheet.getLastRow();
  var con = sourceSheet.getRange(1, 7, sSLastRow); //col that indicates if it should be copied or not
  var conVals = con.getValues(); //con values
  var rangetocopy = sourceSheet.getRange(1, 1, sSLastRow, 6); 
  var rowstocopy = rangetocopy.getValues(); //values to copy
     

  for (var i = 1; i < conVals.length; i++){
    if (conVals[i] !== "CP"){
      targetSheet.appendRow(rowstocopy[i]);
    }
    sourceSheet.getRange(startRow + i, 7).setValue("CP");

  }
}

Try this:试试这个:

function copyRowToTarget() {
  const ss=SpreadsheetApp.getActive();
  const ssh = ss.getSheetByName('Source')
  const tsh = ss.getSheetByName('Target') 
  const startRow = 2; 
  const vals=ssh.getRange(startRow, 6, ssh.getLastRow() - startRow + 1, 2).getValues();
  vals.forEach((v,i)=>{
    if (v[1] != "CP"){
      tsh.appendRow([v[0]]);
      ssh.getRange(startRow + i, 7).setValue("CP");
    }
  });
}

I tested this version:我测试了这个版本:

function copyRowToTarget() {
  const ss=SpreadsheetApp.getActive();
  const ssh = ss.getSheetByName('Sheet1')
  const tsh = ss.getSheetByName('Sheet2') 
  const startRow = 2; 
  const vals=ssh.getRange(startRow, 6, ssh.getLastRow() - startRow + 1, 2).getValues();
  vals.forEach((v,i)=>{
    if (v[1] != "CP"){
      tsh.appendRow([v[0]]);
      ssh.getRange(startRow + i, 7).setValue("CP");
    }
  });
}

Works fine:工作正常:

Data:数据:

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

Sheet2:工作表 2:

17 17
1 1个
20 20
1 1个
23 23
26 26
1 1个
26 26
14 14
13 13

暂无
暂无

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

相关问题 如果值存在于工作表 1 和工作表 2 中,则复制行并将其粘贴到工作表 3。Google 表格。 Google Apps 脚本 - Copy row if value exists in both sheet 1 and sheet 2 and paste it to sheet 3. Google Sheets. Google Apps Script Google表格脚本:自动将一行删除到另一张表格中 - Google Sheets Script : Automatically remove a row into another sheet Google脚本:将行复制到另一张工作表中进行编辑 - Google Script: Copy row to another sheet on edit ALMOST THERE Google Script:尝试将行复制到另一个工作表无法转换错误 - Google Script: trying to copy row to another sheet cannot convert error Google Script:根据值将行从一张纸复制到另一张纸 - Google Script: Copy row from one sheet to another depending on value Google 工作表脚本将数据集从一张工作表复制到另一张工作表的顶部(仅限值) - Google sheets script copy data sets from one sheet to another (values only) at the top of the sheet Google表格将行移动到新表格的脚本 - Script for Google Sheets to Move Row to New Sheet Google 工作表应用程序脚本 - 根据循环条件将数据从一张工作表复制到另一张工作表 - Google sheet app script - Copy data from one sheet to another based on condition with loop 谷歌表格脚本自动定位具有今天日期的单元格,复制整行并通过电子邮件发送 - Google Sheets script to automatically locate cells with todays date, copy the whole row and send it via email 将工作表复制到另一个电子表格[Google Apps脚本] - Copy sheet to another spreadsheet [Google Apps Script]
 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM