I'm a new user of the Google Sheets and could use some help.
I have values in range A2:G2 on my currently active sheet. When the user clicks a button, I want those entered values to be copied into a 2nd sheet ('Sheet4') in the same main spreadsheet on the next open row.
I thought this would work, but the script is not finding the last row or adding the temptext values.
function button1(){
var activesheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var temptext = activesheet.getRange("A2:G2").getValue();
var activesheet2 = SpreadsheetApp.getActiveSpreadsheet();
activesheet2.setActiveSheet(activesheet2.getSheetByName('Sheet4'));
var lastrow = activesheet2.getLastRow(1);
activesheet2.getRange(lastrow + 1,1).setValue(temptext);
}
You need to get and set multiple values, and take care with the parameters to the functions (some take none).
function button1(){
var activesheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var temptext = activesheet.getRange("A2:G2").getValues();
var activesheet2 = SpreadsheetApp.getActiveSpreadsheet();
activesheet2.setActiveSheet(activesheet2.getSheetByName('Sheet4'));
var lastrow = activesheet2.getLastRow();
activesheet2.getActiveSheet().getRange(lastrow+1,1,1,7).setValues(temptext);
}
Related
I am trying to write a script to copy a range of cells to cell B2. The range to copy from changes each time so want to highlight the top left cell and then the script takes it from there. What I have so far is:
function CopyToB2() {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getActiveSheet();
var cellRange = sheet.getActiveCell();
var selectedColumn = cellRange.getColumn();
var selectedRow = cellRange.getRow();
var range = SpreadsheetApp.getActiveSheet().getRange(selectedColumn,selectedRow,2,43);
range.setValues(range);
spreadsheet.getRange('B2').activate();
(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
};
but get the error
Exception: The parameters (SpreadsheetApp.Range) don't match the
method signature for SpreadsheetApp.Range.setValues
Explanation:
The main issue with your code is that you pass a range object in the setValues function and therefore you are getting this error.
A second issue is that you passed the parameters of the getRange(row, column, numRows, numColumns) function in the wrong order. It should be:
getRange(selectedRow,selectedColumn,43,2)
instead of getRange(selectedColumn,selectedRow,2,43)
A third issue is that you need to define the range that the data will be pasted to and that can't be just a single cell B2 in this case. What you need to do is to define B2 as the start of your pasting range and then set the values of your active range.
Solution:
function CopyToB2() {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getActiveSheet();
var cellRange = sheet.getActiveCell();
var selectedColumn = cellRange.getColumn();
var selectedRow = cellRange.getRow();
var dataRange = sheet.getRange(selectedRow,selectedColumn,43,2).getValues();
sheet.getRange(2,2,dataRange.length,dataRange[0].length).setValues(dataRange);
};
I have a Google Sheet that accumulates up to 100 entries per day. As users add new entries, the script puts new data into the last row (see snippet below). However, there are only 12 rows visible on the screen, so once those are filled, the user can't see new entries.
How can I make the last row visible to the user each time data is entered?
I am new to google-apps-script, so thanks for any ideas. I believe onEdit trigger no longer works with sheet.setActiveRange(), so I am looking for a work-around. If there's no simple solution, I'll take a clunky one. For example, can a script be used to hide rows above the last row?
var ss = SpreadsheetApp.getActiveSpreadsheet();
var SHEET_NAME = 'Sheet1' // Name of current sheet
var sheet = ss.getSheetByName(SHEET_NAME);
var dataCell = sheet.getRange('A1');
var lastRowNumber = sheet.getDataRange().getNumRows();
var newData = dataCell.getValue().split(",");
var lastRow = ss.getLastRow();
sheet.appendRow(newData);
Found a solution: setActiveRange(lastRow+2, lastColumn). Scrolls down far enough, without the need to get the range of the appended row. Added advantage is it scrolls before the row is appended, so user sees data as it goes in.
function onEdit(e) { // Puts csv user data into Sheet1
var ss = SpreadsheetApp.getActiveSpreadsheet();
var SHEET_NAME = 'Sheet1' // Name of current sheet
var sheet = ss.getSheets()[0];
var dataCell = sheet.getRange('A1');
var newData = dataCell.getValue().split(",");
// Gets the address of the very last cell of this sheet
var lastRow = sheet.getLastRow();
var lastColumn = sheet.getLastColumn();
// Scrolls to make sure user can see 2 rows below the current last cell
var lastCell = sheet.getRange(lastRow+2, lastColumn);
// Puts user entered data into a new row at the bottom of the spreadsheet
sheet.appendRow(newData);
sheet.setActiveRange(lastCell);
}
You could get the newly added range by adding 1 to the lastrow and activate it:
var lastRow = ss.getLastRow();
sheet.appendRow(newData);
sheet.getRange("A" + (lastRow+1)).activate();//activate the newly appended row's A cell
I'm looking for a code to copy 4 cells for the row that I've selected the cell from one sheet from another sheet of the same spreadsheet, on the last row that is written. The code is this:
function CopiarFila() {
//declaration of sheets
var ss = SpreadsheetApp.openById("ID");
var sheet = ss.getSheetByName("FORD 2019");
var sheetTo = ss.getSheetByName("FORD 2019-F");
//declaration of variables
var row = sheet.getActiveCell().getRow();
var range1 = sheet.getRange("B"+row+":B"+row).getValues();
var range2 = sheet.getRange("C"+row+":C"+row).getValues();
var range3 = sheet.getRange("F"+row+":F"+row).getValues();
var range4 = sheet.getRange("I"+row+":I"+row).getValues();
//declaration of the last row from the new sheet
var ultimaFila = sheetTo.getLastRow();
//adding a row after the last one
sheetTo.insertRowsAfter(ultimaFila, 1);
//copying values to the last row
sheetTo.getRange("B"+ultimaFila+":B"+ultimaFila).setValues(range1);
sheetTo.getRange("D"+ultimaFila+":D"+ultimaFila).setValues(range2);
sheetTo.getRange("C"+ultimaFila+":C"+ultimaFila).setValues(range3);
sheetTo.getRange("H"+ultimaFila+":H"+ultimaFila).setValues(range4);`
}
As I see when running the code, the row is created so the problem isn't from the commands: Sheet, SheetTo and ultimaFila. Am I using properly the command '.getValues('?
I'm stuck and I cannot find a way to fix it. Thank you for your help!
IF you don't mind the blank columns, there is an easier way to do this,
var ss1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("FORD 2019");
var ss2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("FORD 2019-F");
ss1.getRange("B:B").copyTo(ss2.getRange("B:B"));
ss1.getRange("C:C").copyTo(ss2.getRange("D:D"));
ss1.getRange("F:F").copyTo(ss2.getRange("C:C"));
ss1.getRange("I:I").copyTo(ss2.getRange("H:H"));
I want to take the last response from a Google Form and copy it to the same range on another sheet.
The second sheet has some data in columns A & B (a login and password) that I want to assign as somebody completes the registration form.
I want the data from column B to H from the form response sheet to be copied to column C to I on the sheet that contains the login/password columns when a response is received.
I know I am missing the code to take the range from the last row, but I know NO coding at all and my current script is compiled from things I've found around the web and the reference material.
Any advice would be greatly appreciated.
function onEdit(e) {
var spreadsheet = SpreadsheetApp.openById("sheetid");
var sheet = spreadsheet.getSheets()[0];
var lastrow = sheet.getLastRow();
var range = sheet.getRange(2,2,1,7);
values = range.getValues();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var SSsheet = ss.getSheets()[1];
var ssRange = SSsheet.getRange(2,3,1,7);
ssRange.setValues(values);
}
Try using copyTo(destination). It copies the data from a range of cells to another range of cells. Both the values and formatting are copied.
// The code below will copy the first 5 columns over to the 6th column.
var sheet = SpreadsheetApp.getActiveSheet();
var rangeToCopy = sheet.getRange(1, 1, sheet.getMaxRows(), 5);
rangeToCopy.copyTo(sheet.getRange(1, 6));
}
You can also try using copyValuesToRangecopyValuesToRange.
For additional code samples, you may also refer to this forum.
function onEdit(e) {
var spreadsheet = SpreadsheetApp.openById("ID");
var sheet = spreadsheet.getSheets()[0];
var lastRow = sheet.getLastRow();
var range = sheet.getRange("A"+(lastRow)+":G"+(lastRow));
var values = range.getValues();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var SSsheet = ss.getSheets()[1];
var getRow = SSsheet.getLastRow() + 1;
var ssRange = SSsheet.getRange(getRow, 1, values.length, values[0].length);
ssRange.setValues(values);
Try with this,
this code copies the last value you want from a certain range and pastes it in the last available row of the other sheet.
The code it's the same only in the place where you are going to copy it, specify it to be the size of what I want to copy. I hope it helps you :D
I want to focus the last row on a specific sheet (or if possible on every one of them) so here is my function triggered by onOpen on the sheet:
function FocusLastRows() {
var spsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spsheet.getSheetByName("Usuelle_2013");
var rowInt = sheet.getLastRow();
var colInt = sheet.getLastColumn();
var range = sheet.getRange(rowInt, colInt)
sheet.setActiveRange(range)
}
This event works but if my first loaded sheet is different from the one I want it to focus on it just teleports to the other sheet. Best will be to set an .onOpen on the spreadsheet.
Is that possible?
I found a good alternative for it. It's custom menu with function link to it.
//Pierre-luc Des. Script
//Build your menu
function setMenu() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menubuttons = [ {name: "Dernière ligne usuelle", functionName: "GOTOLastRow}];
ss.addMenu("Outils", menubuttons);
}
function GOTOLastRow() {
var spsheet = SpreadsheetApp.getActiveSpreadsheet();
var currentTime = new Date();
var sheet = spsheet.getSheetByName("Usuelle_" + currentTime.getFullYear());
var rowInt = sheet.getLastRow();
var colInt = sheet.getLastColumn();
var range = sheet.getRange(rowInt, colInt);
sheet.setActiveRange(range);
}
Now i will have a clickable menu "Outils" which will let me do the function.
This is rather inelegant, but perhaps try just checking whether the activated sheet is the target sheet? Something like this:
function FocusLastRows() {
var spsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spsheet.getSheetByName("Usuelle_2013");
if (sheet == spsheet) {
var rowInt = sheet.getLastRow();
var colInt = sheet.getLastColumn();
var range = sheet.getRange(rowInt, colInt);
sheet.setActiveRange(range);
}
}
The Google Apps Script documentation seems to indicate that you'll need custom libraries to get any other trigger events than the ones provided, and I'm not sure if it's even in-principle possible to trigger only when a certain specific sheet is activated.
I figure checking the sheet with if comparisons is the next-simplest solution.