problem with condition IF in google javascript - google-apps-script

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()

Related

Use setFormula method with appendRow

I have a Google Apps Script web app that takes values from the request and inserts them in a new row using the appendRow method. Next to inserting the values, I also want to insert formulas in a few columns. Is it possible to do this within appendRow()? Or is there another way to get the row number of the row that was just added, get the cell via getRange and then use the setFormula method?
Please note that I need to keep using the appendRow method and not a workaround via getLastRow due to that multiple request can be made at the same time.
You're going to have to use getLastRow() if you need the last row unless you want to keep track of it yourself.
You can do something like this:
function testtest() {
const ss=SpreadsheetApp.getActive();
const sh=ss.getActiveSheet();
const lr=sh.getLastRow()+1;
const s=`=SUM(A${lr}:C${lr})`;
sh.appendRow([1,1,1,s]);//use whatever values you wish
}
Note: you must be using V8
Values starting with '=' get interpreted as formulas:
In order to set formulas via appendRow, you just need to provide the formula itself, starting with '='. It will get interpreted as a formula. It's the same behaviour than setValue(value):
Sets the value of the range. The value can be numeric, string, boolean or date. If it begins with '=' it is interpreted as a formula.
Example:
sheet.appendRow(["yourRegularValue", "=yourFormula"]);

Google sheet showing formula instead of evaluated result

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

How to find if sheet is empty?

I am getting the data range of a sheet and using Range.getNumRows() to get the number of rows in a Google Spreadsheet using Google Apps Script.
But when the sheet happens to be completely empty, Range.getNumRows() still returns 1 instead of 0. I am guessing this is because a range has to have at least 1 cell.
Is there another (simple) way to get the number of rows in a sheet without having this Problem?
I know I could loop through all cells in the sheet to check it is completely empty, but this doesn't seem very efficient.
I just stumbled across the answer on the app script documentation.
I am using sheet.getLastRow() now
Another option would be to get the sheet range and concatenate it to check if any data is found.
function isSheetEmpty(sheet) {
return sheet.getDataRange().getValues().join("") === "";
}
range.isBlank() work's well too.
Apps Script Range isBlank

Ability to Edit Google Sheets' Custom Function Result

I'm wondering if it is possible to edit the result of a custom function in Google Sheets when returning a 2-dimensional array.
If I try to delete a cell that is part of the returned array, it blinks but stays there.
If I enter something in a cell that is part of the returned 2-dimensional array, it works, but the function then does not return because the value is blocking the display of the 2-dimensional array (Array result was not expanded because it would overwrite data in A3.)
It's the same as for built-in functions returning an array (split, importrange, query, etc): output cannot be edited. But there is a workaround described below.
Suppose the output range is A1:B6. Elsewhere in a sheet, enter =A1 and copy this formula to a 2 by 6 range. This creates a range that is visually identical to the function output, but editable cell-by-cell. If you edit a cell in this new range, it will cease to depend on the function, but the rest will still be updated if the function input changes.

How query/search for information from one Google spreadsheet to another spreadsheet using GAS?

I would like to create a Google Apps Script to do the following: When I select a cell containing the name of a person in a spreadsheet (a spreadsheet that records sales, for example), and use a menu button to call the function, the script does a search (a query) using the person's name (or number of his document) in another spreadsheet that stores complete consumer data and that contains all the information that I need from that consumer to generate a contract or a payment receipt.
What is the best strategy to implement this search for information from one spreadsheet in another spreadsheet using Google Apps Script?
Do you have some script sample with a implementation similar to this? THANK YOU in advance for any help/guidance!
There is no event triggered by a cell selection, you'll have to use a menu item or a button to call the function or, if it is acceptable for your use case, edit the cell to trigger an onEdit event.
The 'search part' is actually very simple, the data being on the spreadsheet itself or in another one has no importance, it will simply change the way you access data ( getActiveSpreadsheet or openById()). From there just get all the values and do a search in the resulting 2D array.
EDIT following your comment : here is an example of such a code that returns the range of the found cell (and we get the A1 notation but we could getValue() as well of course.).
function test(){ // to test the find function with an argument, 'any' in this case
var result = findItem('any');
if(result){Logger.log(result.getA1Notation())}else{Logger.log('no luck !')};
}
function findItem(item){
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var data = ss.getDataRange().getValues()
for(var n = 0;n<data.length;++n){
if(data[n].indexOf(item)>-1){ // this is a "strict" find, ie the value must be the entire search item. If you want to do partial match you should compare differently...
return (ss.getRange(n+1,data[n].indexOf(item)+1)); // if found return the range. note the +1 because sheets have 1 index while arrays have 0 index
}
}
return false;// if we come to the end of sheet without result...
}