简体   繁体   English

使用 Google Apps Script 遍历整个列

[英]Use Google Apps Script to loop through the whole column

I am trying to loop through the whole row in my google sheet and copy some of the data from one sheet to another.我试图遍历我的谷歌工作表中的整行,并将一些数据从一张工作表复制到另一张工作表。 The list will get longer over time.随着时间的推移,列表会变得更长。

More specifically: If input in column B equals "blue", than copy the values from column A and C into another sheet.更具体地说:如果 B 列中的输入等于“蓝色”,则将 A 列和 C 列中的值复制到另一个工作表中。 Do this for all columns till the end of the column.对所有列执行此操作,直到列末尾。

Link to my spreadsheet: https://docs.google.com/spreadsheets/d/1xnLygpuJnpDfnF6LdR41gN74gWy8mxhVnQJ7i3hv1NA/edit?usp=sharing链接到我的电子表格: https : //docs.google.com/spreadsheets/d/1xnLygpuJnpDfnF6LdR41gN74gWy8mxhVnQJ7i3hv1NA/edit?usp=sharing

  • The loop stops when the colour does not equal blue.当颜色不等于蓝色时,循环停止。 Why?为什么?
  • As you can see I used a for loop.如您所见,我使用了 for 循环。 Is that even the way to go?这甚至是要走的路吗?
  • Can I do anything about the speed of the code execution?我可以对代码执行的速度做些什么吗?

Any comments, hints or help are highly appreciated.非常感谢任何评论、提示或帮助。

Regards!问候!

You had the input sheet named "List" and I named the output sheet "Output".你有一个名为“List”的输入表,我将输出表命名为“Output”。 And here's the code.这是代码。

function condCopy()
{
  var s = SpreadsheetApp.getActiveSpreadsheet();
  var sht = s.getSheetByName('List')
  var drng = sht.getDataRange();
  var rng = sht.getRange(2,1, drng.getLastRow()-1,drng.getLastColumn());
  var rngA = rng.getValues();//Array of input values
  var rngB = [];//Array where values that past the condition will go
  var b = 0;//Output iterator
  for(var i = 0; i < rngA.length; i++)
  {
    if(rngA[i][1] == 'blue')
    {
      rngB[b]=[];//Initial new array
      rngB[b].push(rngA[i][0],rngA[i][2]);
      b++;
    }
  }
  var shtout = s.getSheetByName('Output');
  var outrng = shtout.getRange(2,1,rngB.length,2);//Make the output range the same size as the output array
  outrng.setValues(rngB);
}

You have 2 options.您有 2 个选择。 The first is to use the standard query() function from Google Sheets to get the values.第一种是使用 Google Sheets 中的标准 query() 函数来获取值。 The downside here is that it is only a reference of the values.这里的缺点是它只是值的参考。 So you cannot reorder them, etc. To use this, place this in cell A1 and it will pull the Headers and retrieve the values from column A and C:所以你不能对它们重新排序,等等。要使用它,将它放在单元格 A1 中,它将拉出标题并从 A 和 C 列中检索值:

=QUERY(A:C, "select A, C where B = 'blue'", 1)

For a Google Apps Script answer: This will loop through your List sheet and for every row where column B is blue it will save the values in column A and C to column A and B of the new sheet:对于 Google Apps 脚本答案:这将遍历您的列表工作表,对于 B列为蓝色的每一行,它会将 A 列和 C 列中的值保存到新工作表的 A 列和 B 列中:

function doIt(){
  var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet4");
  var lastRow = activeSheet.getLastRow();
  var lastCol = activeSheet.getLastColumn();
  var targetValues = [];

  var sourceSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("List");
  var lastSourceRow = sourceSheet.getLastRow();
  var lastSourceCol = sourceSheet.getLastColumn();

  var sourceRange = sourceSheet.getRange(1, 1, lastSourceRow, lastSourceCol);
  var sourceData = sourceRange.getValues();

  var activeRow = 0;

  //Loop through every retrieved row from the Source
  for (row in sourceData) {
    //IF Column B in this row has 'blue', then work on it.
    if (sourceData[row][1] === 'blue') {
      //Save it ta a temporary variable
      var tempvalue = [sourceData[row][0], sourceData[row][2]];
      //then push that into the variables which holds all the new values to be returned
      targetValues.push(tempvalue);
    }
  }

  //Save the new range to the appropriate sheet starting at the last empty row
  activeSheet.getRange(lastRow + 1, 1 , targetValues.length, 2).setValues(targetValues);
}

Of course, you could pass the value to test to the function by replacing 2 lines.当然,您可以通过替换 2 行将要测试的值传递给函数。 The first, defining the function:首先,定义函数:

function doIt(testingvar){

to pass a variable called testingvar, and the test line to replace the hard coded test with the passed variable:传递一个名为 testingvar 的变量,以及用传递的变量替换硬编码测试的测试行:

if (sourceData[row][1] === testingvar) {

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

相关问题 Google Apps脚本通过范围参数循环 - google apps script loop through range argument 循环浏览一列并根据Google Apps脚本中的单元格值更改另一列的值 - Loop through a column and change value of another column based on cell values in Google Apps Script 循环遍历多维数组并拆分 | Google Apps 脚本 - Loop through multidimensional array and split | Google Apps Script Google Sheets + Apps 脚本:循环通过 ID 移动文件 - Google Sheets + Apps Script: Loop through ID to move files Google Apps脚本-断线循环 - Google apps script - Broken for loop Google App Script/Javascript - 无法使用 Google App Script 循环到 Google 电子表格中的列末尾 - Google App Script/Javascript - Not able to loop through up to the end of Column in Google Spreadsheet using Google App Script 如何定位和迭代 Google 表格/应用程序脚本中的一个特定列(按列名)? - How to target and iterate through just one specific column (by column name) in Google sheets / apps script? 在应用程序脚本中循环遍历Switch语句 - Loop through Switch statement in apps script 我如何使用 Google Apps 脚本解析 XML 并遍历所有元素 - Ho do I Parse XML using Google Apps Script and loop through all elements Google Apps Script - 设置一行仅在循环运行后完成 - Google Apps Script - Setting a row to complete only after it's been ran through the loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM