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
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);
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.
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)
reduce()
.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)
filter()
and when filter()
is used, the 2 dimensional array is returned. In order to return 1 dimensional array, the array is flatten.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)
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})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With