Only get non-filtered values for Google App Script getRange

坚强是说给别人听的谎言 提交于 2021-01-28 05:32:29

问题


Is there a way to only get non-filtered values only in Google App Script? (i.e. get values that are showing and not hidden ones?

For example, let’s say I have the following cells + values in Google Sheets.

A1=abc    B1=x
A2=def    B2=y
A3=ghi    B3=y
A4=kjl    B4=x

And I filtered B column so it only displays [y].

A2=def    B2=y
A2=ghi    B3=y

When I use the following script, both hidden and non-hidden values gets printed to the msgBox.

function msgBoxTest(){

var ss = SpreadsheetApp.getActiveSpreadsheet();
var lastColumn = ss.getLastColumn();
var range_input = ss.getRange("A1:A").getValues();
var result = [i for each (i in range_input)if (isNaN(i))]; 
// remove commas originating from empty cells

Browser.msgBox(result);  
//I want only def & ghi to display here. 
//Instead, I get all values -> abc,def,ghi,kjl
}

I've googled and looked online but couldn't find code about filtering out values in Google App script. Any suggestions?


回答1:


If you want to to use script, this will do it:

function msgBoxTest(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var lastColumn = ss.getLastColumn();
var range = ss.getRange("A1:B").getValues();
var filter=[]//new array
  for(var i=0;i<range.length;i++){
   if (range[i][1]=="y"){
    filter.push(range[i][0])
  }}
Browser.msgBox(filter);  
//I want only def & ghi to display here. 
}

Or you could do it with a simple query formula:

=query(A1:B,"Select A where B='y'")


来源:https://stackoverflow.com/questions/44616109/only-get-non-filtered-values-for-google-app-script-getrange

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!