使用 Google Apps Script 遍历整列 [英] Use Google Apps Script to loop through the whole column

查看:36
本文介绍了使用 Google Apps Script 遍历整列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遍历 Google 工作表中的整行,并将一些数据从一张工作表复制到另一张工作表.随着时间的推移,列表会变得更长.

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.

更具体地说:如果 B 列中的输入等于蓝色",则将 A 列和 C 列中的值复制到另一个工作表中.对所有列执行此操作,直到列末尾.

More specifically: If input in column B equals "blue", than copy the values from column A and C into another sheet. Do this for all columns till the end of the column.

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

  • 当颜色不等于蓝色时循环停止.为什么?
  • 如您所见,我使用了 for 循环.这是要走的路吗?
  • 我可以对代码执行的速度做些什么吗?

非常感谢任何评论、提示或帮助.

Any comments, hints or help are highly appreciated.

问候!

推荐答案

您将输入表命名为List",而我将输出表命名为Output".这是代码.

You had the input sheet named "List" and I named the output sheet "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);
}

这篇关于使用 Google Apps Script 遍历整列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆