问题
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