Google Scripts Custom Function - Array of Ranges as a Parameter - javascript

I am relatively new to creating custom functions in Google Scripts.
I need to be able to pass in an array of ranges into a function.
Please let me know if I am approaching this incorrectly.
I am getting a parse error when I reference it in Google Sheets as =ConcatEach(["A1:A2","B1:B2"],"$"]) (Without the quotes.)
(Also, the values are arbitrary. I know dollar signs go before the value.)
Anyway, here is what I have:
function ConcatEach(rangeString, concatString)
{
//var rangeString = ["A1:A2","B1:B2"];
//var concatString = "$";
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var rangeValues=[];
for(i=0;i<rangeString.length;i++)
{
var range = sheet.getRange(rangeString[i]);
rangeValues.push(range.getValues());
}
//Logger.log(rangeValues);
for(i=0;i<rangeValues.length;i++)
{
for(j=0;j<rangeValues[i].length;j++)
{
if(rangeValues[i][j] != "")
{
rangeValues[i][j] = rangeValues[i][j] + concatString;
}
}
}
return rangeValues;
}
Any help would be super appreciated! Thank you!

you may pass rangeString as string with commas:
"A1:A2,B1:B2"
and then use JavaScript String split() Method:
var array = rangeString.split(",");
Logger.log(array); // you'll get array ['A1:A2', 'B1:B2']

Related

How to check if a sheet cells includes errors or not?

I am using this script function to check if my cell functions in sheet has any errors or not.
Here is the code but it does not seems to be working. It keeps on saying no error when i have an error in a cell
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourcename = "Sheet1";
var source = ss.getSheetByName(sourcename);
var cell = source.getRange("A1:AG30");
function isError2(cell) {
const errorValues = ["#NULL!", "#DIV/0!", "#VALUE!", "#REF!", "#NAME?", "#NUM!", "#N/A","#ERROR!"];
if (errorValues.includes(cell) != true) {
Logger.log("no error");
} else{
Logger.log("some error");
}
}
function isError2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourcename = "Sheet1";
var source = ss.getSheetByName(sourcename);
var cell = source.getRange("A1:AG30");
const errorValues = ["#NULL!", "#DIV/0!", "#VALUE!", "#REF!", "#NAME?", "#NUM!", "#N/A","#ERROR!"];
if (errorValues.includes(cell) != true) {
Logger.log("no error");
} else{
Logger.log("some error");
}
}
Updated the approach but still having no luck with the desired output
var mysheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
var sheet1 = SpreadsheetApp.setActiveSheet(mysheet);
function findErrors(sheet) {
const errorValues = ["#NULL!", "#DIV/0!", "#VALUE!", "#REF!", "#NAME?", "#NUM!", "#N/A","#ERROR!"];
var singleSheetArray = [];
var name = sheet1.getName();
// how many cells in the sheet currently
var maxRows = sheet1.getMaxRows();
var maxCols = sheet1.getMaxColumns();
var totalCells = maxRows * maxCols;
// how many cells have data in them
var r = sheet1.getLastRow();
var c = sheet1.getLastColumn();
var data_counter = r * c;
if (data_counter !== 0) {
var dataRange = sheet1.getRange(1,1,r,c);
var dataValues = dataRange.getValues();
dataValues.forEach(function(row) {
row.forEach(function(cell) {
if ((errorValues.indexOf(cell) === -1) ) {
SpreadsheetApp.getUi().alert("no errors in "+cell);
data_counter --;
}
});
});
}
}
Problem
Unable to check whether the cell has an error
Explanation
The issue you are facing is a simple type mismatch. getRange() method returns an instance of Range, while you try to compare it to a member of a errorValues array, which consists of strings. Therefore, errorValues.includes(cell) will always be false, hence first block of the conditional statement executing.
Solution
Use getValues() on the range, it will return you a 2-dimensional array of values. If you are only interested in one row (which you probably are), extract it and loop over the cells with some (or every) method, doing the same comparison.
Notes
On using global variables in custom functions and in GAS in general. You can use them, GAS environment is a JavaScript runtime with a convenience layer that simplifies working with Google APIs, nearly everything that's valid in JS is valid here. That being said, do treat global variables as if they don't exist - unless you know exactly what you are doing.
References
getRange method reference
getValues method reference
Custom functions guide
every method reference on MDN (see some there)
Try to move the 4 variables inside your function. Apps script does not support global variables. So the function does not recognize the var cell.
EDIT: Detect formula errors in Google Sheets using Script

Setting Values in Google Apps Script

I'm trying to set values using the following logic:
function compare(){
var values = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getDataRange().getValues();
for(n=0;n<values.length;++n){
var cell = values[n][0] ;
values[n][0].setValue("myValue");
}
}
But I can't set the value this way. I'm trying to use the cell[n][x] notation because its easier for what I have to do later on.
Thanks in advance for your help.
You can't use .setValues() because values[n][0] is not an object, it is just the value of a 2D-array. Just use values[n][0] = "my Value". At the end you will have editted your array, then you'll need to use range.setValues() to set the new values to your range.
Example:
function compare(){
var myRange = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getDataRange();
var values = myRange.getValues();
for(var n=0;n<values.length;n++){
values[n][0]="myValue "+n;
values[n][1]="myValue 2 "+n;
}
myRange.setValues(values)
}

jQuery and XML, find object that contains children with multiple specific values

For a project I have data stored in XML like this:
<xml>
<sprites>
<sprite>
<name>Tile1</name>
<lat>1</lat>
<lng>2</lng>
</sprite>
<sprite>
<name>Tile2</name>
<lat>3</lat>
<lng>4</lng>
</sprite>
</sprites>
<xml>
Through jQuery I want to get a tile object that matches two child values, the lat and lng values.
I found this post which was of great help, but sadly it only has an example of how to search for one matching value. Here's the code I have up to now:
// xml stored in 'xml' var
var findLat = 3;
var findLng = 4;
var mapSprites = $(xml).find("sprites");
var getSprite = $(mapSprites).find("sprite").filter(
function() {
return $(this).find('lat').text() == findLat;
},
function() {
return $(this).find('lng').text() == findLng;
}
);
Sadly getSprite is undefined, as I'm guessing you can't use the filter function as I've tried to use it? The example I linked to has one function as filter and seems to work, but comma separating doesn't seem to work as an AND, which is what I need.
The goal is to be able to give the function a lat and lng value and me being able to extract the <name> value.
Would be thankful for a push in the right direction, I'm pretty new to XML and parsing it through jQuery.
filter does not take multiple arguments. So combine it into one using "and".
var findLat = 3;
var findLng = 4;
var mapSprites = $(xml).find("sprites");
var getSprite = mapSprites.find("sprite").filter(
function() {
const node = $(this);
return Number(node.find('lat').text()) === findLat &&
Number(node.find('lng').text()) === findLng;
}
);

Adding values to a specific property in an array(?) in javascript

Im wondering how I can add a value to to main in this array.
var herpderp = {
"main": ["stuff", "stuff"]
};
so it would look like:
var herpderp = {
"main": ["stuff1", "stuff2", "stuff3"]
};
Preferably I'd like to create this kind of structure from a couple of strings in a fucntion if that's possible. So says I have this
var strings = {"stuff1","stuff2", "stuff3"}
for (each element of strings) {
what do I do here to get the structure above
}
Alternatively another function to search through an Array of objects on a specifik property. Right now Im trying to use this to filter through the array, maybe there's another way?
var arrays = search;
var result = events.filter(function(item) {
for (var prop in arrays)
if (arrays[prop].indexOf(item[prop]) == -1)
return false;
return true;
});
Thank you kindly for any reply!
herpderp.main is your array, so you can add to the array like so:
herpderp.main.push(value)
If the key for your array was not a valid variable name (it starts with a number, for example), you could use bracket notation instead:
herpderp['123key'].push(value)
Actually, you are changing the previous values beside adding a new element.
var herpderp = {
"main": ["stuff", "stuff"]
};
var strings = ["stuff1","stuff2", "stuff3"];
strings.forEach(function(value,key){
herpderp.main[key] = value;
});
console.log(herpderp);

Google Script to see if text contains a value

I have a google form that when the user submits it will trigger my function to run which is creating a summary of what they submitted as a Google Doc. I know it can automatically send an email but I need it formatted in a way that my user can edit it later.
There are some check boxes on the form -- but the getResponse() is only populated with the items checked and I need it to show all possible choices. Then I will indicate somehow what was checked.
I can't find a way to see if a text contains a value.
Like in Java with a String, I could do either .contains("9th") or .indexOf("9th") >=0 and then I would know that the String contains 9th. How can I do this with google scripts? Looked all through documentation and I feel like it must be the easiest thing ever.
var grade = itemResponse.getResponse();
Need to see if grade contains 9th.
Google Apps Script is javascript, you can use all the string methods...
var grade = itemResponse.getResponse();
if(grade.indexOf("9th")>-1){do something }
You can find doc on many sites, this one for example.
Update 2020:
You can now use Modern ECMAScript syntax thanks to V8 Runtime.
You can use includes():
var grade = itemResponse.getResponse();
if(grade.includes("9th")){do something}
I had to add a .toString to the item in the values array. Without it, it would only match if the entire cell body matched the searchTerm.
function foo() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName('spreadsheet-name');
var r = s.getRange('A:A');
var v = r.getValues();
var searchTerm = 'needle';
for(var i=v.length-1;i>=0;i--) {
if(v[0,i].toString().indexOf(searchTerm) > -1) {
// do something
}
}
};
I used the Google Apps Script method indexOf() and its results were wrong. So I wrote the small function Myindexof(), instead of indexOf:
function Myindexof(s,text)
{
var lengths = s.length;
var lengtht = text.length;
for (var i = 0;i < lengths - lengtht + 1;i++)
{
if (s.substring(i,lengtht + i) == text)
return i;
}
return -1;
}
var s = 'Hello!';
var text = 'llo';
if (Myindexof(s,text) > -1)
Logger.log('yes');
else
Logger.log('no');

Categories