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.
Related
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)
I am new to Google App Script and I have a very basic question.
The following code gives me an error "Range not found". What I want to do is to fill an array with values (which are in the google sheet). Then I want to work with the values.
Function is linked to a button:
function today(){
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName('Sheet1');
var range = sheet.getRange("A1:B2");
var values = range.getValues();
sheet.setActiveSelection(values[1, 1]);
Adding Your Own Values
Here's how you can add your own values to a sheet.
function addMyValues(){
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Sheet1');
var values=[[1,2,3],[4,5,6],[7,8,9]];//this is a square matrix
sh.getRange(1,1,values.length,values[0].length).setValues(values);//Creates a range with origin at row 1 column 1 and number of rows in values and number of columns in values
}
When using setValues() it's always inportant to give it the correct range size or you will get errors.
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()
I was trying to write a script in google spreadsheets that, will read a range from a defined cell, take its value and Insert as many rows as the range would indicate, and then copy and paste an specific range also given by a range value in a cell.
Apparently the copy and paste range value works, but the range value for the insertRowsBefore is unable to convert the range value, any idea why this happens? I get this error
cannot convert (range) to class
here is how the scripts looks like:
function CopyActualvsWMS() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("ActualVsSystem");
var value0 = sheet.getRange('T4');
var r1 = value0.getValue();
var insert =ss.getSheetByName("DataBase").insertRowBefore(r1);
var value1 = sheet.getRange('T3');
var cr1 = value1.getValue();
var source = ss.getRange(cr1);
source.copyTo(ss.getRange("DataBase!a2"), {contentsOnly: true});
}
The problem is coming from this line:
var insert =ss.getSheetByName("DataBase").insertRowBefore(r1);
You are putting a variable name r1 into the parameter, but the parameter must be a number (integer), not a string or a range reference. The variable r1 doesn't evaluate to an integer. It's probably a string.
Google Documentation - insertRowBefore()
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