Google Scripts Generated VLOOKUP Formula Does not Calculate Properly - google-apps-script

I am writing formulas to cells programmatically.
The following line of code:
formulaCells.setFormulaR1C1('=iferror(VLOOKUP(R[0]C[3],AutoCat!A:B,2,FALSE),"Requires Category")');
Adequately writes this formula into all the cells in the target Google Sheets file represented by formulaCells
=iferror(VLOOKUP(D2,AutoCat!A:B,2,FALSE),"Requires Category")
But the problem is that the formula defaults to the error flag "Requires Category" when it is written by Google Scripts, but if I write the very same formula manually into Google Sheets, the actual item identified by the VLOOKUP results.
This is so frustrating!
If I hover over the Google Script generated formula, the true solution from the VLOOKUP even appears in the flyover, but for some reason, does not appear in the cell.
Please help! Why will a Google Scripts generated formula not calculate properly wen the same formula entered manually will?

OK, after thinking I'd exhausted my mental resources on this, I tried one last thing, and it actually worked.
For some unknown reason, though the formula is accurate, Google Scripts generated formulas do not play well with a VLOOKUP which searches an unlimited range for a solution. So even though the VLOOKUP was finding the correct solution, it was not displaying it as the formula result.
I fixed this by creating in the Google Sheets file a named range of the data the VLOOKUP would search called AutoCategory, and then inserted that named range into the Google Scripts generated formula, and BOOM! the formula started working.
Here is the final code in Scripts:
formulaCells.setFormulaR1C1('=iferror(VLOOKUP(R[0]C[3],AutoCategory,2,FALSE),"Requires Category")');
And here is the final formula which is generated in Google Sheets:
=iferror(VLOOKUP(D2,AutoCategory,2,FALSE),"Requires Category")

setFormulaR1C1 requires R1C1 notation, which you provided with R[0]C[3], but AutoCat!A:B is A1 notation.
You could switch to setFormula() and pass in only A1 notation, but I think that using named ranges is a very good practice when combined with Google Apps Script.

Related

How to prevent GOOGLEFINANCE from recalculating?

I'm using a GOOGLEFINANCE formula, but it keeps updating. I would like it to stay a fixed amount once it has been calculated the first time but I can't seem to figure out a way to do that. I tried Googling for the answer but came up empty-handed. I went to File > Spreadsheet Settings > Calculation, but there doesn't seem to be an option there to prevent recalculations, there only exist options to delay them.
It seems that feature is not built into Google Sheets. But maybe someone who is really good at formulas or a script. Can think of a super smart way to do it with a formula or a script?
There is no way to stop a Google Sheets formula, including those that use GOOGLEFINANCE function to recalculate.
You might replace the formula by it's result. One simple way to do this is by doing copy-paste as value only.
P.S. GOOGLEFINANCE data can't be retrieved by Google Apps Script.
Related
Pulling current price change % (Google Finance) with Google Apps Script

Google Sheets script general mechanics

I have put together a rather large script file that does a lot of math. It seems to me that if I place a script variable value on the spreadsheet (setValues()), the whole spreadsheet recalcs (i.e.; it takes awhile to refresh). Is that true?
What if I want to format a cell from script (e.g.; change a number cell to a percentage cell with 3 places)[FormattedSS.getRange(rangecoordinates).setNumberFormat("#.###%");]? When I set the format, does the whole spreadsheet recalculate?
I am new to Google Sheets. In Excel, I could set calculation off. Google sheets does not seem to have that option.
Google sheets has no manual calculation option. You would need to create a formula to check whether to run the rest of the formula. If the output is a single cell you can easily use a circular reference to retain the value.
When it comes to Google App Scripts every time you execute a script there will be an annoying delay, that is just how it works, the App Script should be avoided at all cost in a normal spreadsheet except for when actually necessary.
Google App Script Server -> Google Sheet (delay to send information back and forth)
Google Sheet always recalculates after any change

Can we call the function "Show formulas" in Google app script for Spreadsheet

I am currently working on a script for a google spreadsheet. This script compares two sheets together and applies changes based on the differences between these sheets. First, I take the data from the sheets and put them in an array then i compare them and apply the changes before pasting them on their original sheets.
My problem is that there are formulas that fill the some cells of my sheets. However, when I run my script, it only reads the data that appears in the cells and so the formulas are deleted once the script is finished. However I'd like to keep these formulas on my sheets ..
To solve the problem I "show formulas" (Ctrl + `) before launching the script, so the script reads the formulas instead of the cells' value.
I would like to call this function "Show Formulas" (Ctrl + `) from my google script, is it possible? If yes, how?
Thank you in advance,
Victor
Yes, it is possible.
For example, instead of getValues() you can use getFormulas(), and later on, when you paste the data into the destination sheet: setFormulas()
Another possibility is two copy-paste the data into a temporary sheet with copyTo(destination, copyPasteType, transposed) chosing the copyPasteType PASTE_FORMULA
You can also use the Advanced Sheets Service and retrieve and set values with the valueRenderOption FORMULA

Google Sheets filename in cell via formula instead of a script?

Is there a FORMULA that will display the name of the file in a cell?
I've found scripts that will do it, formulas that will display the sheet name, but no luck finding a formula that will show the filename.
If I have to resort to the script, so be it. But I'd like to use formula if possible.
If this has been asked before, please point me to the post and I will delete this one.

Google sheets Named ranges not consistent between sheets

I have two similar spreadsheets that share code in an apps script library.
I use named ranges to access the sheets from the code.
In one spreadsheet the ranges are sheetname!range like this:
and the other spreadsheet just uses range:
I have tried copying both spreadsheets and tried recreating ranges to match the other format but nothing seems to work. I have looked for something that tells me that the spreadsheets are different versions but cant find anything that is different. I can make the code look for either format but.....
If you copy a worksheet in a spreadsheet with a Named Range, then Google Sheets has to do something, as you can't have two Named Ranges with the same name in the same spreadsheet. What it does is create new Ranges with worksheet-level scope of the form: newworksheet!existingnamedrange. So I think that probably explains how you got to where you are.
In terms of fixing it? You can just delete the newworksheet!existingnamedrange name and recreate existingnamedrange. As you can have the same name in different spreadsheets. Or am I misunderstanding the problem?