Range not found - google-apps-script

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.

Related

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.

How to multiply a cell by 100 and copy the resulting value into the last column of another sheet

Having trouble with very simple code, I have successfully copied cells and ranges of cells to other sheets, but I cannot get the multiplication of a value to copy, I am sure this is very simple so apologize up front.
The error I get is "TypeError: Cannot find function getcopyTo in objec"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[1];
var nextsheet = ss.getSheets()[4];
var lastColumn = nextsheet.getLastColumn()+1;
var newData = sheet.getRange("D35").getValue()*100;
newData.getcopyTo(nextsheet.getRange(9,lastcolumn), {contentsOnly:true});
var newData = sheet.getRange("D35").getValue()*100;
This returns a number, like 4200. Then you try newData.getcopyTo which can't possibly work, there is no method "getcopyTo" in the number 4200. (Actually, there is no such method in Apps Script, it looks like some mix of getValues and copyTo). What you should do is
nextsheet.getRange(9,lastcolumn).setValue(newData);
The method copyTo is used in the form sourceRange.copyTo(targetRange) but that's direct copying from one range to another, without any manipulation like multiplying by 100.

appendRow not putting data into Google spreadsheet

What am i doing wrong here? All it does is create the new file but nothing is in the new spreadsheets.
function summarize_projects() {
var sheet = SpreadsheetApp
.openByUrl(theURL).getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
//var sheet1 = sheet.getRangeByName('GUILLERMO');
//var sheet2 = sheet.getRangeByName('GLORIA');
//var sheet3 = sheet.getRangeByName('PV');
var ssNew = SpreadsheetApp.create("BDATA1");
Logger.log(rows.getValues);
var ssNew1 = SpreadsheetApp.create("BDATA2");
ssNew1.appendRow(values);
Logger.log(rows.getValue());
var ssNew2 = SpreadsheetApp.create("BDATA3");
ssNew2.insertSheet('BDATA3', 'yassir');
Logger.log(rows.getValue());
The method getValues() gets a two dimensional array of data. The appendRow() method can not take a two dimensional array. And you can't use the appendRow() method in this case. You are getting multiple rows of data, and appendRow() can not write multiple rows of data.
You must use this code:
ssNew1.getRange(1,1,values.length, values[0].length).setValues(values);
The above code starts in row one and column one. If you don't want that. If you want the data to be inserted at the end. You will need to use:
ssNew1.getRange(ssNew1.getLastRow() + 1,1,values.length, values[0].length).setValues(values);
But since this is a brand new spreadsheet, there shouldn't be anything in the sheet to begin with.
The syntax for this particular version of getRange() is:
getRange(Starting Row, Starting Column, Number of Rows to Write, Number of Columns to Write)

Copy a row to new sheet based on value in a cell

I'm trying to create a Google Script that copies rows from one Google Sheet into different sheets based on the value of a cell.
The cell value are states in the United States. The master spreadsheet has data from a registration form that is being imported into it all the time. When a new registration happens (and the data is imported into the master sheet), I'd like the script to run and copy that row of data into the appropriate state sheet.
Here's where I'm at:
Get master sheet
Find the last row
Get the value of the cell in the column "state"
Copy that row into one of 50 different sheets depending on what state it is.
Run the script every time the master sheet is updated (via an API).
Any help would be appreciated. I'm definitely a newbie when it comes to scripting and this is just hurting my head.
Here's the code I have so far:
function myFunction() {
// Get Source Spreadsheet
var source = SpreadsheetApp.getActiveSpreadsheet();
// Get Source Sheet from Spreadsheet
var source_sheet = source.getActiveSheet();
// Get Active Range from Sheet
var lastRow = sheet.getLastRow();
// Get the Value of the State Cell
var cellValue = Range.getCell(lastrow,3);
// Copy Last Row to Appropriate State Sheet
if ( cellValue == 'Alaska') {
var target = SpreadsheetApp.openById("");
var target_sheet = target.getSheetByName("Sheet1");
target_sheet.appendRow(lastRow);
}
}
With this code:
// Get Active Range from Sheet
var lastRow = sheet.getLastRow();
The getLastRow() method returns an integer. Then further down in your code, you are using the lastRow variable as the data for appendrow() which won't work;
target_sheet.appendRow(lastRow);
The code should probably be something like this:
var target = SpreadsheetApp.openById("");
var target_sheet = target.getSheetByName("Sheet1");
var lastRowOfData = source_sheet
.getRange(source_sheet.getLastRow(), 1, 1, source_sheet.getLastColumn())
.getValues(); //Returns a two dimensional array
var oneD_Array = lastRowOfData.join().split(","); //Creates a one D array
target_sheet.appendRow(oneD_Array);
The appendRow() method takes a one dimensional array. The getValues() method returns a 2 Dimensional array, so it must be converted. Because you are only getting one row of data, it's easy to convert. The outer array only has one inner array. The one inner array has all the cell values of the row.
Here's the answer for what I came up with for the code:
function myFunction() {
// Get Source Spreadsheet
var source = SpreadsheetApp.getActiveSpreadsheet();
// Get Source Sheet from Spreadsheet
var source_sheet = source.getActiveSheet();
// Get Last Row
var lastRow = source_sheet.getLastRow();
// Get Last Column
var lastColumn = source_sheet.getLastColumn();
// Get Last Row of Data
var lastRowOfData = source_sheet.getRange(lastRow, 1, 1, lastColumn).getValues();
// Creates a one dimensional array
var oneD_array = lastRowOfData.join().split(",");
// Get the Value of the State Cell
var cellValue = source_sheet.getRange(2,7).getValue();
// Copy Last Row to Appropriate State Sheet
if ( cellValue == "New York" ) {
var target = SpreadsheetApp.openById("");
var target_sheet = target.getSheetByName("Sheet1");
target_sheet.appendRow(oneD_array);
}
}