简体   繁体   English

如何获取正在循环的数组的行号?

[英]How to get row number of an array that's being looped through?

Using Google AppScripts and found this script online which I've modified.使用 Google AppScripts 并在网上找到了我修改过的这个脚本。

NOTE: Spreadsheet has headers.注意:电子表格有标题。

  1. Run through the values found in 3rd column运行在第 3 列中找到的值
  2. If any of the values include the word "USD", copy the entire row into "Target Sheet"如果任何值包含“USD”一词,请将整行复制到“目标表”中
  3. Delete that row after it's finished copying复制完成后删除该行
  4. Continuing looping through until it finds the next "USD"...etc.继续循环直到找到下一个“美元”……等等。

What Works: I'm successfully able to loop through the array and copy the correct rows into the "Target Sheet"什么有效:我能够成功地遍历数组并将正确的行复制到“目标表”中

What I Need Help With: I can't figure out how to delete the row from the original sheet.我需要什么帮助:我不知道如何从原始工作表中删除该行。 It always ends up deleting the row before, then skips 1 row every time it loops again.它总是最终删除之前的行,然后每次再次循环时跳过 1 行。 I've tried multiple different formats to this portion of the code, such as i-1, i+1, etc... Not sure what I'm doing wrong here:我已经对这部分代码尝试了多种不同的格式,例如 i-1、i+1 等……不确定我在这里做错了什么:

if (i == 0) {
  ss1.deleteRow(i+2);
} else {
ss1.deleteRow(i)
}

I've pasted the entire script below:我在下面粘贴了整个脚本:

var etfar = ["Cash File"] //This is a string because I have multiple sheets I'm looping through
 
function cashCopy(etf) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var ss1 = ss.getSheetByName(etf);
  var ss2 = ss.getSheetByName("Target Sheet");
  var lr = ss1.getLastRow();
  var lc = ss1.getLastColumn();

// gets the data in Cash File
var range = ss1.getRange(1, 1, lr, lc);
var data = range.getValues();

// loops through rows in data
for (var i = 0; i < data.length; i++) {
  var check = data[i][2] // ith row, 3rd column
  if (check.includes("USD")) { 

 var rowToCopy = data[i];
 ss2.appendRow(rowToCopy);
    
    if (i == 0) {
      ss1.deleteRow(i+2);
    } else {
    ss1.deleteRow(i)
                 }

  }; 
}; // end i
}

    for (var i = 0; i < etfar.length; i++) {
    cashCopy(etfar[i])
  }

Explanation:解释:

One way to iteratively delete rows in a sheet is to create a backwards for loop.迭代删除工作表中的行的一种方法是创建一个向后的for循环。

Replace :替换

for (var i = 0; i < data.length; i++)

with :

for (var i = data.length - 1; i >= 0; i--)

In this way, every time you delete a row, the data will still correspond to the correct row.这样,每次删除一行, data仍然会对应到正确的行。


Another issue is that you get var range = ss1.getRange(1, 1, lr, lc) .另一个问题是你得到var range = ss1.getRange(1, 1, lr, lc) That means, you start iterating from the first row (including the headers) and then you are using a workaround like that:这意味着,您从第一行(包括标题)开始迭代,然后您正在使用这样的解决方法:

if (i == 0) {
  ss1.deleteRow(i+2);
} else {
ss1.deleteRow(i)
             }

But actually, you don't need to include the headers in the first place.但实际上,您不需要首先包含标题。 Use this instead: var range = ss1.getRange(2, 1, lr, lc) and replace the if/else statement with just: ss1.deleteRow(i+2) .改用它: var range = ss1.getRange(2, 1, lr, lc)并将if/else语句替换为: ss1.deleteRow(i+2)

since data index starts from 0 , but your range starts from row 2 .因为data索引从0开始,但您的range从第2行开始。


Solution:解决方案:

var etfar = ["Cash File"] //This is a string because I have multiple sheets I'm looping through
 
function cashCopy(etf) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var ss1 = ss.getSheetByName(etf);
  var ss2 = ss.getSheetByName("Target Sheet");
  var lr = ss1.getLastRow();
  var lc = ss1.getLastColumn();

// gets the data in Cash File
var range = ss1.getRange(2, 1, lr, lc); // <- modification
var data = range.getValues();

// loops through rows in data
for (var i = data.length - 1; i >= 0; i--)
 {
  var check = data[i][2] // ith row, 3rd column
  if (check.includes("USD")) { 

 var rowToCopy = data[i];
 ss2.appendRow(rowToCopy);
    
 ss1.deleteRow(i+2); // <- new code
    
  }; 
}; // end i
}

    for (var i = 0; i < etfar.length; i++) {
    cashCopy(etfar[i])
  }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM