Google spreadsheet script cell parameters - google-apps-script

Hi im having trouble passing 2 cells in my scripts parameters:
function myFunction(c1, c2) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[1]; //This is the correct sheet
var valueFirstCell = sheet.getRange(c1).getValue();
var valueSecondCell = sheet.getRange(c2).getValue();
return valueSecondCell - valueFirstCell;
}
So i use this in cell A3:
=myFunction(A1, A2)
And lets say A1=10 and A2=15 i would like A3 to show 5 but,
var valueFirstCell = sheet.getRange(c1).getValue();
fails "Argument has to be a range"
Any ideas?

In the custom function, c1 and c2 in your script are actually going to be the value of what's in those cells, so you can just use them directly. You can see that by adding Logger.log(c1); and Logger.log(c2); to your script. Try this:
function myFunction(c1, c2) {
Logger.log(c1);
Logger.log(c2);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[1]; //This is the correct sheet
return c2 - c1;
}
There's a tutorial here on building a simple custom function that shows a similar example.

It's juste saying that argument of getRange function is not in the way it's expected.
link to getRange() documentation

getRange() function expects row and column.
For e.g. valueFirstCell is in 7th row and 1st column.
var valueFirstCell = sheet.getRange(7,1).getValue();
Link to getRange() official documentation

Related

Call cell data from different spreadsheets WITHOUT having to go to the sheet and select the cell

I would like to create a function in script editor that would allow me to input data from a cell on another sheet without having to put 'sheet1!A1' in the formula.
Say I have my sheet name in A1 and want to return the number in the first row and first col (i.e. A1) of that sheet. I would like to show that value with a formula as simple as getcell(A1,1,1)
Here's what I've got but I get an error saying "Exception: Range not found (Line 4)"
function getcell(sheetName,row, col) {
var ss = SpreadsheetApp.getActive()
var range = ss.getSheetByName(sheetName).getRange(row, col)
ss.getRange(range).getValue();
}
I believe your goal is as follows.
You want to return A1 as A1Notation by putting =getcell(A1,1,1) to a cell.
You want to achieve this using the custom function.
In this case, how about the following modification?
Modified script:
function getcell(sheetName, row, col) {
var ss = SpreadsheetApp.getActive()
var range = ss.getSheetByName(sheetName).getRange(row, col);
var res = range.getA1Notation();
return res;
}
In your script, range is used in getRange(range). By this, such an error occurs.
In order to return the A1Notation, getA1Notation() is used.
If you want to return the cell value, please modify getA1Notation() to getValue().
Reference:
getA1Notation()
Three small corrections:
You need to pass the sheet name in quotes
You are trying to get the range twice
For a custom funciton, you need to return the value
Sample code:
function getcell(sheetName,row, col) {
var ss = SpreadsheetApp.getActive()
var range = ss.getSheetByName(sheetName).getRange(row, col)
return range.getValue();
}
Sample call:
=getcell("Sheet1",1,1)

Add custom formulas to Google sheets using Apps script

I'm trying to calculate to cells but not working. This is my code :
function total() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("sheet1");
var cell = sheet.getRange("A3");
var calculate = cell.setFormula("=SUM(A1+A2)");
return calculate
}
My cell is A1 AND A2 I want to put result in cell A3 by add "=total()".
not working but if run from script it work.
I want to put =total() in cell A3 and work without see my formula in sheet.
I wand to send copy of my sheet to people and he can read and edit it.
I don't need people to see my formulas.
or any another way to hide my formulas from people
You need to calculate the result in the script:
function total() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("sheet1");
var values = sheet.getRange("A1:A2").getValues();
var value = values[0][0] + values[1][0];
return value;
}
Alternatively, you can
Calculate normally with formulas in another spreadsheet
Use IMPORTRANGE to import the data from that spreadsheet such that only values but not formulas are display in the cells
However, he cannot make changes to the range directly.
He need to first copy and paste by values.
Your custom formula is returning the return value of the function Range.setFormula(formula), which is a Range. And you want a number. For this you need the values of both cells "A1"and "A2".
Furthermore, if you are using this custom formula to set a value in cell "A3" and not a formula then you can just have the custom formula return the value.
Example:
function total() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("sheet1");
var values = sheet.getRange("A1:A2").getValues();
var value = values[0][0] + values[1][0];
return value;
}

Copying from Spreadsheet to another Spreadsheet error

I am a total beginner using appscript, can someone please help me to resolve this error.
The parameters (String) don't match the method signature for SpreadsheetApp.Range.setValues
function Input1() {
var sss = SpreadsheetApp.openById('10xap9qOf487MB9M9FQ-Turp81X_ZIoDVMyFY-Puf9BU');
var ss = sss.getSheetByName('Input'); // ss = source sheet
var values = ss.getRange("A2:H2").getValue();
var tss = SpreadsheetApp.openById('1thJ5uqhfwZhdxsE0B9MhyhjwcFZpe4A0Ty3erGIuWpU');
var ts = tss.getSheetByName('Entries'); // ts = target sheet
ts.getRange(ts.getLastRow()+1,1,1,8).setValues(values);
}
You are trying to take the values in the Range A2:H2 on Sheet "Input", and copy them to the row following the last row on Sheet="Entries".
The problem is in this line of code:
var values = ss.getRange("A2:H2").getValue();
Instead of using getValue(), you should use getValues()
Your range is 8 columns wide:
getValue() "returns the value of the top-left cell in the range". In this case it returns the value of a single cell=A2.
getValues() will return "the rectangular grid of values for [the] range". This is just what you want so that you can use it with the setValues() method in the last line of the script.

Need help Google Apps Script and sheet: Building a custom function for Google spreadsheet to Copy/Paste Value

I'm building a function to get Value from another cell. It will work this way:
At cell A1, I place function = copyValue(B1) or = copyValue(1 , 2) so that custom function will return the value of B1.
Here is my code:
function copyValue(int a,b) {
var ss = SpreadsheetApp.openById('1HTEuyd7po43VKM37nmzCvPgLEVESgcN5YpAu2VRTiFI');
var sheet = ss.getSheetByName("Total");
var cell = sheet.getRange(a,b);
var c = cell.getValue();
return(c);
}
But when I run, it said:
[quote]Missing ) after formal parameters. (line 1)[/quote]
if I remove "int" before the formal params, it said I cannot getRange NULL.
Please help me to fix this. Thanks
You can make your function much simpler if all you are trying to do is get the value of a cell.
function copyValue(cell) {
return cell;
}
Then in your sheet just do =copyValue(B1) and whatever your value is in B1 will be returned into the cell.

When using setFormulas I get error Cannot find function error "object function getFontColor() {/* */}."

I'm trying to create a script that copies down formula the length of the sheet (stitched together on the back of other SO posts I found while researching):
var originRange = ss.getSheetByName('data_prep').getRange('C6:I6');
var formulas = originRange.getFormulas();
var target = ss.getSheetByName('data_prep').getRange('C7:I');
for(i in target) {
target[i].setFormulas(formulas);
}
When I run this function I get:
TypeError: Cannot find function setFormulas in object function getFontColor() {/* */}.Dismiss
What I had hoped/expected was for all the formulas in C6:I6 to be copied into each row starting C7 down the length of the sheet.
The function works for one row at a time. So if I set target as just C7:I7 and get rid of the for loop it works.
Why am I getting this getFont error?
How can I get my script to copy the formulas in C6:I6 down the entire sheet?
As an alternative to 2, how can I get my script to copy down as far as while there is data in column A?
Maybe try something like this:
var ss= SpreadsheetApp.getActiveSpreadsheet();
var dataPrepSheet = ss.getSheetByName('data_prep')
var originRange = dataPrepSheet.getRange('C6:I6');
var everyRow = dataPrepSheet.getMaxRows().toString();
var target = dataPrepSheet.getRange('C7:I' + everyRow);
originRange.copyTo(target);
SpreadsheetApp.flush();
Use the getMaxRows() method to get every every row in the spreadsheet, whether it's got content or not. That returns an integer, so convert it to a string then use a text formula to create the A1 notation.
Then, don't set the formulas, use copyTo()