Here's my problem : i'm trying to copy a value from one cell in "sheet1" to another cell in "sheet2".
But I want to run this script everyday (using trigger) because the value change every day. So the paste has to be in B2 the first day, B3 the second day, etc. I want to be able to follow the value day by day and have an historic.
Here's my current script :
function backup(){
var sourceSheet = "Portfolio" ;
var sourceRange = "N15" ;
var targetSheet = "Suivi perfs journalières" ;
var targetRange = "B2:B999" ;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sourceSheet);
var values = sheet.getRange(sourceRange).getValues();
ss.getSheetByName(targetSheet).getRange(targetRange).setValues(values);
I have 2 problem :
My script send me this error : Exception: The number of rows of data does not match the number of rows in the range. There are 1 lines of data, but the range has 998. (line 12, "CopyPastePortfolioValue" file)
I don't know how to paste the value in a new cell everyday (paste in the cell under the previous each day)
Thanks for your help!
I believe your goal as follows.
You want to retrieve the value from the cell N15 in the source sheet and append the value to the target sheet.
Modification points:
When setValues(values) is used, the lengths of rows and columns of values are required to match to the range. In your script, values of var values = sheet.getRange(sourceRange).getValues(); is 1 row and 1 column. But the target range is B2:B999. By this, such error occurs. I think that this is the answer for your 1st question.
In order to append the value to the target sheet, in your case, I would like to propose to use appendRow. I think that this is the answer for your 2nd question.
And, when a value from a cell, you can also use getValue instead of getValues.
When above points are reflected to your script, it becomes as follows.
Modified script:
function backup(){
var sourceSheet = "Portfolio";
var sourceRange = "N15";
var targetSheet = "Suivi perfs journalières";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sourceSheet);
var value = sheet.getRange(sourceRange).getValue(); // Modified
ss.getSheetByName(targetSheet).appendRow([value]); // Modified
}
References:
setValues(values)
appendRow(rowContents)
Related
I'm trying to put together a script that cuts a range from a google spreadsheet and pastes it to another spreadsheet. The script works fine as long as the source sheet is the first sheet in my document. How can I fix it so that the script grabs the right sheet no matter where it is in my document?
I'm very very new at this and mostly used code I found online. Probably made a very obvious mistake..
function CopyRangeTo_OtherSheet() {
var sss = SpreadsheetApp.openById('1O1gML-IeOkx6n6RhCavay6Zg9zflalcVs0qkB2yu7bE');
var ss = sss.getSheetByName('Sheet1');
var range = ss.getRange(2,4,sss.getLastRow()-1,9);
var data = range.getValues();
var tss = SpreadsheetApp.openById('1WHoETM3QxV166ndnPW4Qv37Y6SBGPVw-CcEQxNpw0Co');
var ts = tss.getSheetByName('Sheet3');
ts.getRange(ts.getLastRow()+1, 1,50,9).setValues(data);
var spread = SpreadsheetApp.openById('1O1gML-IeOkx6n6RhCavay6Zg9zflalcVs0qkB2yu7bE');
var sheet = spread.getSheetByName("Sheet1");
sheet.getRange("D2:H").clearContent();
}
The number of rows will be different in each sheet. .getLastRow() on a spreadsheet class only returns the last row in first sheet. getLastRow of sheet instead
ss.getLastRow()
Your line var ss = sss.getSheetByName('Sheet1'); implies that you take the sheet with the name Sheet1.
This is the sheet you use to obtain your values from range and subsequently copy them.
If you want to copy / cut and paste values from a different sheet, simply paste the correct name of the sheet in var ss = sss.getSheetByName('PASTE HERE THE NAME OF THE SHEET');
Small annotation:
If the range you want to clear is the same from which you copied the
values, you do not need to define it again. You can simply replace the
lines
var spread = SpreadsheetApp.openById('1O1gML-IeOkx6n6RhCavay6Zg9zflalcVs0qkB2yu7bE');
var sheet = spread.getSheetByName("Sheet1");
sheet.getRange("D2:H").clearContent();
through
range.clearContents();
Also note:
setValues() only works if the range into which the values shall be set has the same dimension like the value range, i.e. the original range.
If your original range is 2,4,ss.getLastRow()-1,9, then the number of rows is ss.getLastRow()-1 and the number of columns 9.
Thus, you need to define
ts.getRange(ts.getLastRow()+1, 1,ss.getLastRow()-1,9).setValues(data);
to adapt the row number dynamically.
I am trying to use the script editor to allow me to execute the function stored in cell A1 every 24 hours. I want the value that is generated to be stored in cell A2, then the next time the script runs I want that value stored in A3, and so it goes. The problem I am having is that I can't get my code to do this.
I have tried editing the last few lines of the script, but I can not get it to work.
function recordChanges() {
var url = 'LINK TO MY SHEET';
var ss = SpreadsheetApp.openByUrl(url);
var sh = ss.getSheetByName('Sheet1');
var range = sh.getRange("A1");
var values = range.getValues();
var lastRow = sh.getLastRow();
if (values[0][0] != values[1][0]) {
sh.insertRowAfter(lastRow);
sh.getRange().setValue(values[0][0]);
}
}
I expect the script to store the value, and the next time it runs I want it to store the value on the cell below the last store valued. Bonus points if you can get it to place the date the new value was generated in the same row but in column B.
getLastRow only returns the last row of the sheet that contains content.
I am interpreting your question as "how can I copy the value of A1 to the next empty cell in row 1?" My apologies if I am misinterpreting your question, please let me know.
It is unclear what you mean by "execute formula on last row"; please clarify and perhaps show the formula. This code does not make A1 change: I am assuming you already have the value of A1 changing somehow, and all you need to do is copy the current value to the end of row one.
function recordChanges() {
var url = 'LINK TO MY SHEET';
var ss = SpreadsheetApp.openByUrl(url);
var sh = ss.getSheetByName('Sheet1');
var range = sh.getRange("A1");
var firstRow = range.getDataRegion(SpreadsheetApp.Dimension.COLUMNS);
var values = firstRow.getValues();
values[0].push(values[0][0]);
sh.getRange(1, 1, 1, firstRow.getLastColumn() + 1).setValues(values);
}
I have written a script on Google App Script that will, at one point, get a range from one sheet, copy the values on that range and paste them to another sheet.
I have done so by doing this:
var range = sh.getDataRange().offset(0, 0);
var data = range.getValues();
var ts = SpreadsheetApp.openById('SHEETID').getSheetByName('SHEETNAME')
ts.getRange(ts.getLastRow() + 1, 1, data.length, data[0].length).setValues(data)
I would like, however, to specify the exact range to copy and another exact range to paste the data into.
I would like the range to be copied/pasted to be from columns A to Z whilst still keeping the getLastRow() in the script.
How would I do that?
Thanks for your help.
You can use getRange(a1Notation) to fetch a range and its contents. I usually go for A1 notation. Sample:
// Get a range A1:D4 on sheet titled "Invoices"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var range = ss.getRange("Invoices!A1:D4");
// Get cell A1 on the first sheet
var sheet = ss.getSheets()[0];
var cell = sheet.getRange("A1");
There are other variations of getRange you can use. Choose what you're comfortable with.
For copying and pasting altogether, you can use the copyTo(destination)
// The code below copies the first 5 columns over to the 6th column.
var sheet = SpreadsheetApp.getActiveSheet();
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);
}
}
I have been searching for a way to copy certain cells from a row and paste them into another spreadsheet, but all I can seem to find are ways to do that just from sheet to sheet within one spreadsheet or questions that deal with code way above my head. I'm just now learning to code in Google Spreadsheets and I can't seem to get it right. Here is my code for now, I'm working on just copying one cell first and after I can do that I'll get a loop to iterate through the row and pick the cells I want to copy.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var master = SpreadsheetApp.openById('0AgcCWQn-aoI1dFpLVE4tSENwcThrYnlMUzhuRmdWU2c');
var target = SpreadsheetApp.openById('0AgcCWQn-aoI1dF96X2dBT2dVVFZ2SU1NRWdYTDJhT2c');
var master_sheet = master.getSheetByName("Steve2");
var target_sheet = target.getSheetByName("Corbin1");
var master_range = master_sheet.getRange("A1");
var target_range = target_sheet.getRange("A1");
master_range.copyTo(target_range);
Right now it is giving me an error saying that it cannot call the getRange method of null. I'm not sure if I'm using OpenById correctly or if I can even use that in this situation.
OK I've got it working now. It copies the row perfectly and it copies only the cells I want. Now I need help with the pasting part. It copies everything fine and puts it into the other sheet great, but I need it to copy into the next available row and into the first available columns. So if row 5 is the last column of data, I need it to paste into row 6 and take up the first 6 or so columns.
This is what I have so far.
function menuItem1() {
var sss = SpreadsheetApp.openById('0AgcCWQn-aoI1dFpLVE4tSENwcThrYnlMUzhuRmdWU2c'); // sss = source spreadsheet Steve2
var ss = sss.getSheetByName('Sheet1'); // ss = source sheet
//Message box asking user to specify the row to be copied
var answer = Browser.inputBox('What row would you like to copy?');
var range = parseInt(answer);
var array = [1,2,3,9,12,30];
//Runs a loop to iterate through array, using array elements as column numbers
for(var i = 0; i < array.length; i++){
var SRange = ss.getRange(answer,1,1,array[i]);
//get A1 notation identifying the range
var A1Range = SRange.getA1Notation();
//get the data values in range
var SData = SRange.getValues();
}
var tss = SpreadsheetApp.openById('0AgcCWQn-aoI1dF96X2dBT2dVVFZ2SU1NRWdYTDJhT2c'); // tss = target spreadsheet Corbin1
var ts = tss.getSheetByName('Sheet1'); // ts = target sheet
//set the target range to the values of the source data
ts.getRange(A1Range).setValues(SData);
//Confirmation message that the row was copied
Browser.msgBox('You have successfully copied Row: ' + answer);
}
You are getting this error maybe because your spreadsheet does not have a sheet called Steve2 or Cobin1. Try using the method master.getSheets()[0], this way you will get the first sheet without using their name.
You can algo use this piece of code to check the sheets names:
for(var x in master.getSheets()) {
Logger.log(master.getSheets()[x].getSheetName());
}
best,