Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 295
user1717390 Avatar asked Jun 16 '17 01:06

user1717390


3 Answers

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);
like image 154
TripleDeal Avatar answered Nov 19 '22 02:11

TripleDeal


  • You want to achieve the following result.

    • Input

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

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

In the current stage, I thought that although the comprehensions of var result = [i for each (i in range_input)if (isNaN(i))] can be still used, it is not suitable for this situation as tehhowch's comment. Alto I think that filter() is suitable for this situation. In this update, I would like to update this by proposing other solution. If this was useful, I'm glad.

Pattern 1:

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Test"); 
var range_input = ss.getRange("A1:A").getValues();
var result = range_input.reduce(function(ar, e) {
  if (e[0]) ar.push(e[0])
  return ar;
}, []);
Logger.log(result) // ["abc","def","uvw","xyz"]
Browser.msgBox(result)
  • In this pattern, the empty rows are removed by reduce().

Pattern 2:

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Test"); 
var range_input = ss.getRange("A1:A").getValues();
var result = [].concat.apply([], range_input).filter(String); // or range_input.filter(String).map(String)
Logger.log(result) // ["abc","def","uvw","xyz"]
Browser.msgBox(result)
  • In this pattern, the empty rows are removed by filter() and when filter() is used, the 2 dimensional array is returned. In order to return 1 dimensional array, the array is flatten.

Pattern 3:

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Test"); 
var range_input = ss.getRange("A1:A").getValues();
var criteria = SpreadsheetApp.newFilterCriteria().whenCellNotEmpty().build();
var f = ss.getRange("A1:A").createFilter().setColumnFilterCriteria(1, criteria);
var url = "https://docs.google.com/spreadsheets/d/" + ss.getId() + "/gviz/tq?tqx=out:csv&gid=" + sheet.getSheetId() + "&access_token=" + ScriptApp.getOAuthToken();
var res = UrlFetchApp.fetch(url);
f.remove();
var result = Utilities.parseCsv(res.getContentText()).map(function(e) {return e[0]});
Logger.log(result) // ["abc","def","uvw","xyz"]
Browser.msgBox(result)
  • In this pattern, the empty rows are removed by the filter, then the filtered values are retrieved.

Result:

enter image description here

References:

  • reduce()
  • filter()
  • map()
  • Class Filter
like image 42
Tanaike Avatar answered Nov 19 '22 02:11

Tanaike


Not tested for efficiency, but for a simple removal of multiple ,s, you can use regex:

const a=[['abc'],['def'],[''],[''],[''],['xyz'],['']];//simulate getValues()
const out =  a.join(',').replace(/,+(?=,)|,*$/g,'') //'abc,def,xyz'
const out2 = a.join('«').replace(/«+$/,'').split(/«+/) //flattened

console.log({out, out2})
like image 23
TheMaster Avatar answered Nov 19 '22 03:11

TheMaster