How to ignore empty cell values for getRange().get

2019-02-19 04:01发布

问题:

I am able to get the range values using getValues() and put it into a string by declaring the following variables in Google App Script

var ss = SpreadsheetApp.getActiveSpreadsheet(); 
var sheet = ss.getSheetByName("Test"); 
var range_input = ss.getRange("A1:A").getValues();

However, I realize I am getting a lot of commas in my string probably from all the empty calls.

For example, if values are following

================
Spreadsheet("Test") Values

A1=abc
A2=def
A3=    
A4=
A5=
A6=uvw
A7=xyz
================

If I do msgBox, it gets something like below.

Browser.msgBox(range_input) // results = abc,def,,,,uvw,xyz,,,,,,,,,,,

Is there a way to remove the trailing commas so I get something like below? (i.e. ignore the empty cells)

Browser.msgBox(range_input) // results = abc,def,uvw,xyz

回答1:

How about this sample?

Sample :

var ss = SpreadsheetApp.getActiveSpreadsheet(); 
var sheet = ss.getSheetByName("Test"); 
var range_input = ss.getRange("A1:A").getValues();

var result = [i for each (i in range_input)if (isNaN(i))];
Browser.msgBox(result)



回答2:

You can also use filter().

var ss = SpreadsheetApp.getActiveSpreadsheet(); 
var sheet = ss.getSheetByName("Test"); 
var range_input = ss.getRange("A1:A").getValues();
var filtered_input = range_input.filter(String);