[英]Get the values of particular columns and filter on a particular column - Google Apps Script
I have this script我有这个脚本
function filter2() {
var sss = SpreadsheetApp.getActiveSpreadsheet(); //replace with source ID
var ss = sss.getSheetByName('Users'); //replace with source Sheet tab name
var range = ss.getRange('A:G'); //assign the range you want to copy
var rawData = range.getValues() // get value from spreadsheet 1
var data = [] // Filtered Data will be stored in this array
for (var i = 0; i< rawData.length ; i++){
if(rawData[i][6] == "User missing") // Check to see if Column E says No Billing Accountif not skip it
{
data.push(rawData[i])
}
}
var tss = SpreadsheetApp.openById('hsgsklllssdllsls'); //replace with destination ID
var ts = tss.getSheetByName('Summary'); //replace with destination Sheet tab name
ts.getRange(ts.getLastRow()+1, 1, data.length, data[0].length).setValues(data);
}
This is a filter script.这是一个过滤器脚本。 In this filter is getting value from source in the range A:G, but i need to get only Column B, Column D, Column F, Column G在此过滤器中,从 A:G 范围内的源获取值,但我只需要获取 B 列、D 列、F 列、G 列
How to avoid getting filtered Column A,C,E?如何避免过滤列 A、C、E?
One way to select particular columns is to use the map function. select 特定列的一种方法是使用map function。
The following will get columns b,d,f,g
from the range A:G
.以下将从范围A:G
中获取列b,d,f,g
。
const rawData = ss.getRange("A:G").getValues().map(([,b,,d,,f,g]) => [b,d,f,g]);
Don't use a for
loop to filter on a particular column.不要使用for
循环来过滤特定列。
It is way more efficient to use filter to filter on a particular column.使用过滤器过滤特定列的效率更高。 You haven't specified in your question for which column you want to filter on.您尚未在问题中指定要过滤的列。 Assuming the new data will consist of columns b,d,f,g
the column indexes for the raw data will be 0,1,2,3
.假设新数据将由列b,d,f,g
组成,原始数据的列索引将为0,1,2,3
。
For example:例如:
const frawData=rawData.filter(row=>row[3]=="User missing");
this will filter on column g
since we have removed a,c,e
and column g
is in 3rd
position (starting from 0
).这将过滤g
列,因为我们已经删除a,c,e
并且g
列在3rd
position 中(从0
开始)。 If you want to filter on a different column.如果要过滤不同的列。 Choose 0
for b
, 1
for d
, 2
for f
and 3
for g
.为b
选择0
,为d
选择1
,为f
选择2
,为g
选择3
。
function filter2() {
const sss = SpreadsheetApp.getActiveSpreadsheet(); //replace with source ID
const ss = sss.getSheetByName('Users'); //replace with source Sheet tab name
const rawData = ss.getRange("A:G").getValues().map(([,b,,d,,f,g]) => [b,d,f,g]); // new code (gets b,d,f,g)
const frawData=rawData.filter(row=>row[3]=="User missing");// 3 means column G. [b,d,f,g] => [0,1,2,3]
const tss = SpreadsheetApp.openById('hsgsklllssdllsls'); //replace with destination ID
const ts = tss.getSheetByName('Summary'); //replace with destination Sheet tab name
ts.getRange(ts.getLastRow()+1, 1, frawData.length, frawData[0].length).setValues(frawData);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.