Google sheet showing formula instead of evaluated result - google-apps-script

Thanks for looking into this
I have an App script custom function as below which return a formula for a cell. When I use this function from a cell in google sheet it shows the formula =Sum(1,2) instead of the evaluated result i.e 3.
function testReturningFormula(){
return "=Sum(1,2)";
}

If you set a cell with formula =testReturningFormula(), it cannot overwrite the cell's formula you've created. It can only return a string or array. You should have the formula itself parse the arguments you wish.
This should help:
https://developers.google.com/apps-script/guides/sheets/functions

The custom function is not returning a formula, it's returning a string.
It's not possible to use a custom function to return a formula, they only are able to return values. Reference https://developers.google.com/apps-script/guides/sheets/functions

Related

Can't sort Google Sheet by column which value is a result of Google Apps script function

I created Google Sheet where the value of one column is a formula
=getRank(ROW()) where getRank() is a function defined in Google Apps Script as (the logic of the function is not important, it returns some integer that represent rank of the row that depends on the data in the row):
function getRank(iRow) {
...
return <some rank>;
}
When I try to sort the sheet by this column - I see some random results and the column's itself order doesn't change. How can it be? If I do the same logic by a formula entered in this column instead of a formula with a function call - sorting works as usual.

Passign parameters form google sheet to google script

I am new to google scripts and I am having trouble with passing parameters from my google spreadsheet to my google script.
I have created a function in my script:
function functionname(ref) {
Logger.log(ref);
var value = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(ref).getValue();
//do stuff with the value read from the sheet
return modified_value;
}
and I am calling it in my google spreadsheet like:
=functionname(A4)
(the function is called from an empty A5 cell, with cell A4 filled with text).
I have initially tried with a static reference in the function, like:
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange("H9").getValue()
In this situation, it worked perfectly, but I do not want to replicate the function for each cell I need to operate, given I need to deliver this to non-programmers and the list of cells will increase.
Initially, I read that passing the parameters from the cell will be sufficient to receive the value and I had tried also this solution. Still, no matter what I try, the value of ref is always "undefined".
Can anyone please help me understand what I am missing?
Thanks
No need for the SpreadSheetApps methods for custom function to pull the value from the SpreadSheet because it already gets the cell value that you are referencing. Just remove the var value = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(ref).getValue();
function functionname(ref) {
Logger.log(ref);
//do stuff with the value read from the sheet
return modified_value;
}
You can call this function directly on your spreadsheet. Don't forget to store the new value on a variable and return it, or return the computed value directly.

problem with condition IF in google javascript

I need to do research if the value of one cell is present in another sheet.
I retrieve the value of the searched cell then browse all the rows of my sheet2.
I can display the values, but my IF condition is not working. It is never taken into account.
club=feuilleClub.getRange(j,1).getValues();
clubComp=feuilleLic.getRange(i,5).getValues();
if (club == clubComp) {
Browser.msgBox("Find");
feuilleLic.getRange(i, 10).setValue(clubNom);
}
Thanks for your help
You are using getValues() instead of getValue(). The getValues() method will return a two dimensional array hence, the comparison is not accurate.
You should update your code to this:
club=feuilleClub.getRange(j,1).getValue();
clubComp=feuilleLic.getRange(i,5).getValue();
Reference
Class Range Apps Script
getValues()
Class Range Apps Script
getValue()

How to convert a cell reference into a string in Google Spreadsheet Apps Script?

Is there a google apps script function that allows you to extract the string from the reference in a cell?
For instance suppose my cell contains the value:
=A1:A5
The value that results from this reference will be the value in A1.
What I want to know is whether a function exists to get the string value of the reference; i.e. convert =A1:A5 to "=A1:A5" or more preferably "A1:A5", where "=A1:A5" and "A1:A5" are strings.
After you get cell you can just use .getFormula().
Reference:
https://developers.google.com/apps-script/reference/spreadsheet/range

Google script setValue

I'm trying to learn how to manipulate with cell in Google spreadsheets with Google script. I have learned, that
Custom functions return values, but they cannot set values outside the cells they are in. In most circumstances, a custom function in cell A1 cannot modify cell A5. However, if a custom function returns a double array, the results overflow the cell containing the function and fill the cells below and to the right of the cell containing the custom function. You can test this with a custom function containing return [[1,2],[3,4]];
So I'm calling function in cell C14
function test(input){
var secondCell = SpreadsheetApp.getActiveSheet().getRange("C14").setValue("Ahoj");
return secondCell.getValue();
}
and still getting error
You do not have permission to call setValue
I can't even set data in cell from which I'm calling the function.
Does anybody know why is this not working ?
Edit:
I've read possible duplicate Why can't you use setValue in a custom function?
But this is not solving my problem.
I don't want to edit other cells. I only want to edit the original cell containing the formula. According the quoted text, I should be possible to edit cell, if it's original cell containing the formula. But my example is returning error even though it's accessing only itself.
Your code makes no sense, you set the value progamaticly in C14 to "Ahoj", then get this already know variable, and try to return it to set in the cell that you just tried to set the value to "Ahoj", which would replace the function you just used to run this very function. That's almost a paradox.
Your making a confusion, you can get the value of C14, but that function can't be called in C14, or if you want to set the value of C14 to "Ahoy", in that function all you need is:
function test(input){
return 'Ahoj';
}
If you want another cell to have the value of C14 then:
function test(input){
return SpreadsheetApp.getActiveSheet().getRange('C14').getValue();
}
The returned value will be the new cell value, it isn't done trough setValue.