简体   繁体   English

获取特定列的值并过滤特定列 - Google Apps 脚本

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

Explanation:解释:

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

Solution:解决方案:

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.

相关问题 Google Apps 脚本 - 想要截断表单中特定列中的文本 - Google Apps Script - Want to truncate a text in a particular column in a form sheet UrlFetchApp 服务器使用 Google Apps 脚本锁定特定电子表格 - UrlFetchApp server locks up on a particular spreadsheet with Google Apps Script 如何从谷歌应用程序脚本中的数组中检索特定行? - How to retrieve a particular row from an array in google apps script? 如何使用Google App脚本获取Google表格中特定列的最后一个单元格? - How to get last cell of a particular column in google sheet using google app script? 仅按谷歌应用程序脚本中的列设置值 - Set Values by only columns in google apps script 在Google Apps脚本中获取总值 - Get total values in google apps script 重构我的coffeescript代码以获取特定列值的总和 - Refactoring my coffeescript code to get sum of particular columns values Google Script GetRange with Filter based on values in a column AND select only certain columns - Google Script GetRange with Filter based on values in a column AND select only certain columns 如何获取kendo网格特定列的所有值? - How to get all the values of particular column of kendo grid? Google Apps脚本从电子表格的2列中串联2个值-在列表框中显示 - Google Apps Script Concatenate 2 values from 2 columns in Spreadsheet - display in listbox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM