Google sheets script always returning same respone - google-apps-script

Still quite new to this.
I have written a function:
function maxHousing(actualCost,allowedCost){
if (actualCost == ""){
return "";
}
if (parseInt(actualCost) <= parseInt(allowedCost)){
return actualCost;
}
else{
return allowedCost;
}
}
I then have a field in my sheet with the formula:
=ARRAYFORMULA(
if(
row(Parents!A:A)=1,
"Allowed Housing",
maxHousing(Parents!D1:D,Allowances!B6)
)
)
This function always returns allowedCost, no matter what I make actualCost.
I'm clearly doing something really obviously wrong here!

Related

My code in Apps script is taking too much time to execute

If the user sets value of C2 as Message Finder then my cell should go to c77. I have such say 9-10 cases. I have added 3 cases in the code.
But, it is taking too long and my sheet shows "Running script for more than 8-10 seconds.
My aim is to reduce it to just 1-2 seconds or at least better than current situation
function getTool()
{
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Course");
if(sheet.getRange("c2").getValue() == "Message Finder")
{
sheet.setActiveCell("Course!T95");
}
else if(sheet.getRange("c2").getValue() == "Fee Finder")
{
sheet.setActiveCell("Course!U39");
}
else if(sheet.getRange("c2").getValue() == "Fee Message")
{
sheet.setActiveCell("Course!N39");
}
}
Please help. Thanks
Still see no big problem with your code.
Though probably you can speed up the code this way:
function getTool() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Course');
var cell = sheet.getRange('c2').getValue();
if (cell == 'Message Finder') { sheet.getRange('t95').activate(); return }
if (cell == 'Fee Finder') { sheet.getRange('u39').activate(); return }
if (cell == 'Fee Message') { sheet.getRange('n39').activate(); return }
}

Doesn´t recognice as equal (==)

Can someone tell me why these variables marked with red are not recognized as equal (==).
Google Apps Script is Javascript-based. In Javascript, you can not compare two arrays using ==.
One method is to loop over both arrays and to check that the values are the same. For example you can include the function:
function compareArrays(array1, array2) {
for (var i = 0; i < array1.length; i++) {
if (array1[i] instanceof Array) {
if (!(array2[i] instanceof Array) || compareArrays(array1[i], array2[i]) == false) {
return false;
}
}
else if (array2[i] != array1[i]) {
return false;
}
}
return true;
}
And then update the line in your code from if (responsables == gestPor) { to if (compareArrays(responsables, gestPor)) {
For other methods of comparing arrays in Javascript, see this answer.
It is because you are comparing arrays. If you are just getting a single cell value, use getValue() instead of getValues()
To make things work, change these:
var gestPor = hojaActivador.getRange(i,13,1,1).getValues();
var responsables = hojaConMails.getRange(1,n,1,1).getValues();
to:
var gestPor = hojaActivador.getRange(i,13).getValue();
var responsables = hojaConMails.getRange(1,n).getValue();
Do these to all getValues() where you're only extracting 1 cell/value.
See difference below:

Missing ) after formal parameters. (line 1, file "Code")

I am trying to make a count down clock using a user input to start it on Google Apps Script (Which uses JavaScript). And I'm getting the error (I think it is Syntax):
"Missing ) after formal parameters. (line 1, file "Code")"
Here is my code:
function COUNTD(Browser.inputBox(prompt))
{
var clock = Browser.inputBox("Intiate countdown sequence?");
if(clock == "yes")
{
return("T - 30 minutes and counting...");
}
if(count == "abort")
{
return("Countdown aborted")
}
}
I know there are multiple variations of this question, but I couldn't user the answers given for those and apply them to mine.
Am I just being really stupid?
I think what you need here is something like this:
function COUNTD(bInputBox)
{
var clock = bInputBox("Intiate countdown sequence?");
if(clock == "yes")
{
return("T - 30 minutes and counting...");
}
if(count == "abort")
{
return("Countdown aborted")
}
}
COUNTD(Browser.inputBox);

Google spreadsheets custom function code not working: =RootDomain

Hi I am trying to program a Google spreadsheets custom function. It receives a range and spits out the clean rootdomain. I just ran into an "too many executions" - I have to run this on my whole sheet. So I added a range.
Now the feedback is "internal error function" ....
Help appreciated .... this must be possible!
/**
* Generates clean root domains
*
* #param {input} input The value to change to a root domain.
* #return The clean root domain.
* #RootDomain
*/
function RootDomain(input) {
if (input == null) return '';
if (input.map) { // Test whether input is an array.
return input.map(RootDomain); // Recurse over array if so.
} else {
if (input = '') return '';
regex = new RegExp(/((www)\.)?.*(\w+)\.([\w\.]{2,6})/);
return regex.exec(input)[0].replace(/^http(s)?:\/\//i, "").replace(/^www\./i, "").replace(/\/.*$/, "");
}
}
Do this instead:
function RootDomain(input) {
if (input == null || input === '') {
return '';
} else if (input.map) { // Test whether input is an array.
return input.map(RootDomain); // Recurse over array if so.
}
var regex = new RegExp(/((www)\.)?.*(\w+)\.([\w\.]{2,6})/);
return regex.exec(input)[0].replace(/^http(s)?:\/\//i, "").replace(/^www\./i, "").replace(/\/.*$/, "");
}

Find existing bookmark folder by title?

I am trying to bring back the folder id of any bookmark folder which has a title matching a given string.
Problem is it doesn't return back folder ids when the text is the same :C
This is my code:
chrome.bookmarks.getTree(function(bookmarks)
{
search_for_url(bookmarks, "herpaderp");
});
function search_for_title(bookmarks, title)
{
for(i=0; i < bookmarks.length; i++)
{
if(bookmarks[i].url != null && bookmarks[i].title == title)
{
// Totally found a folder that matches!
return bookmarks[i].id;
}
else
{
if(bookmarks[i].children)
{
// inception recursive stuff to get into the next layer of children
return search_for_title(bookmarks[i].children, title);
}
}
}
// No results :C
return false;
}
There are two problems with your search_for_title function.
The variable i must be local. To make it a local variable, you have to use var i = 0 instead of i = 0 in the for statement.
search_for_title returns false when it can't find a bookmark that has the specified title, but you still need to look into the next item, so after recursively calling search_for_title, you return the return value only if the bookmark has been found. Otherwise, search should be continued instead of returning false.
Here's the code I tested to run correctly:
function search_for_title(bookmarks, title)
{
for(var i=0; i < bookmarks.length; i++)
{
if(bookmarks[i].url != null && bookmarks[i].title == title)
{
// Totally found a folder that matches!
return bookmarks[i].id;
}
else
{
if(bookmarks[i].children)
{
// inception recursive stuff to get into the next layer of children
var id = search_for_title(bookmarks[i].children, title);
if(id)
return id;
}
}
}
// No results :C
return false;
}