[英]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
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.