I want to know if we can select a row in a Spreadsheet using Google script like this
Anyway first I've tried with the columns by using this fonction but I get an error of the data (is not found): If you have an idea :)
function testGetFullColumn()
{
getFullColumn('A', 1) ;
}
// This function gets the full column Range like doing 'A1:A9999' in excel
// #param {String} column The column name to get ('A', 'G', etc)
// #param {Number} startIndex The row number to start from (1, 5, 15)
// #return {Range} The "Range" object containing the full column: https://developers.google.com/apps-script/class_range
function getFullColumn(column, startIndex){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var lastRow = sheet.getLastRow();
return sheet.getRange(column+startIndex+':'+column+lastRow);
}
Edit :I know about getRange but I would be cool^^^^^^ to see in visually the selection like in the photo I've attached because I tried with
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; return sheet.getDataRange();
and it doesn't select all the sheet visually I mean I want to see an interactive selection :) like here
Edit 2: I've tried to use setActiveRange after getRange as you suggested thanks Guilherme M but it doesn't select visually all the document :( Your code works it's really cool^ I was afraid that it's impossible to select I will try to do the same thanks ^^
function testGet()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getDataRange();
SpreadsheetApp.setActiveRange(range)
}
Edit 3: To select all the document I've tried this code:
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lastRow = sheet.getLastRow();
var lastColumn = sheet.getLastColumn();
var range = sheet.getRange(1,1,lastRow+1,lastColumn+1);
sheet.setActiveRange(range);
var selection = sheet.getSelection();
// Current cell: A1
var currentCell = selection.getCurrentCell();
// Active Range: tout le document
var activeRange = selection.getActiveRange();
Yes, you can select a row or any range in the spreadsheet using Apps Script. You do so with method setActiveRange(). An example follows below:
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var range = sheet.getRange('A1:D4');
sheet.setActiveRange(range);
var selection = sheet.getSelection();
// Current cell: A1
var currentCell = selection.getCurrentCell();
// Active Range: A1:D4
var activeRange = selection.getActiveRange();
Notice The method getDataRange() selects range "in which data is present.", so it won't select blank cells or the whole document. For such task, you need method getRange(row, column, numRows, numColumns) combined with getMaxRows() and getMaxColumns().
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var range = sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns());
sheet.setActiveRange(range);
var selection = sheet.getSelection();
// Current cell: A1
var currentCell = selection.getCurrentCell();
// Active Range: A1:D4
var activeRange = selection.getActiveRange();
To do that you show in the image i use this:
function myFunction2() {
SpreadsheetApp.getActive().getRange( 2 + ':' + 2 ).activate();
};
and if you want to make it variable so, add a variable like n:
function myFunction2() {
var n = 2
SpreadsheetApp.getActive().getRange( n + ':' + n ).activate();
};
the way to manage range, rows and columns, are in the other answer.
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 copy the data of Sheet1 to Sheet2 with the code below.
When I inserted an arrayformula(A2:A&B2:B) in Sheet2, the sript stopped working and i got the error:
coordinates of the target range are outside the dimensions of the
sheet array formula
When I deleted the arrayformula, the execution of the script completed successfully.
What should i change to the code (or to the arrayformula) to get them to cooperate?
function dailyLog() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName('Sheet1');
var logSheet = ss.getSheetByName('Sheet2');
var lastRow = logSheet.getLastRow();
var range = sourceSheet.getDataRange();
range.copyTo(logSheet.getRange(lastRow + 2, 1), {contentsOnly: true}); // paste under previous paste
}
Issue:
The input range and the output range do not match:
Input range: range = sourceSheet.getDataRange()
Output range: logSheet.getRange(lastRow + 2, 1)
The output range is a single cell, while the input range is the full data range (therefore multiple cells).
Solution:
If the input range is dynamic it is a better idea to use getValues/setValues instead:
function dailyLog() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName('Sheet1');
var logSheet = ss.getSheetByName('Sheet2');
var lastRow = logSheet.getLastRow();
var values = sourceSheet.getDataRange().getValues();
logSheet.getRange(lastRow+1,1,values.length,values[0].length).setValues(values);
}
and now the size of the data (values) is dynamically set with the help of the length function.
Reference:
getValues()
setValues()
I'm trying to make a script that will copy a cursor selection range to an other sheet, script is partially working
The script is coping some values, some of the values selected from the target sheet
function copy(){
var sh = SpreadsheetApp.getActiveSheet();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var range = ss.getActiveRange();
var srange = range.getA1Notation();
var crange= ss.getRange(srange);
var data = crange.getValues();
var tss = SpreadsheetApp.openById('id');
var ts = tss.getSheetByName('Tracker');
ts.getRange('B16:K200').setValue(data)
}
The script should copy the selected value with the mouse from the selected sheet in the specified location for the target sheet
The key is to identify the activerange, and the relevant get row and column values. Then using "B16" as a starting point for the outout, you simply add the number of Rows and number of Columns calculated from the activerange.
Highlight any range, run the function, and the contents of the range will be copied to the same sheet with cell "B16" in the top lefthand corner.
It's a simple matter to modify the code to create the output range and copy the values to another spreadsheet.
function so5666559701() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetname = "56665597";
var sheet = ss.getSheetByName(sheetname);
var myrange = sheet.getActiveRange();
var myvalues = myrange.getValues();
//Logger.log("DEBUG: the active range = "+myrange.getA1Notation());
var myfirstRow = myrange.getRow();
var mylastRow = myrange.getLastRow();
var myfirstCol = myrange.getColumn();
var mylastCol = myrange.getLastColumn();
//Logger.log("DEBUG: details of the selectred range: row: first row="+myfirstRow+", first column="+myfirstCol+", last row="+mylastRow+" last column:"+mylastCol);
// target cell = B16 = row 16, column 2
var targetrange = sheet.getRange(16,2,mylastRow-myfirstRow+1,mylastCol-myfirstCol+1);
//Logger.log("DEBUG: the target range = "+targetrange.getA1Notation());
targetrange.setValues(myvalues);
}
I need to enter data in a Google sheet cell, which cell is in a named range. To do that using a Google Script, I need to know the named range's column number. I have written a script that gets the job done, but only because I know the named range has only one column, so I use the getLastColumn command to get the column number. I know there has to be a command to "getColumnNumber" or something like that, but from Google documentation and this site, I simply can't find it. Any help? Bound script below:
function addSong() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet();
var lastRow = sheet.getLastRow()
sheet.appendRow([lastRow+1]);
SpreadsheetApp.flush();
var range = sheet.getRange(sheet.getLastRow(), 1);
var songTitle = Browser.inputBox('New Song', 'Enter the song title', Browser.Buttons.OK_CANCEL);
var namedRange = sheet.getRange("Title");
var col = namedRange.getLastColumn();
sheet.getRange([lastRow+1], col).setValue(songTitle)
SpreadsheetApp.setActiveRange(range);
}
Hey Cooper: I tried your approach and it works great, except it loses some of what I was doing with my script; specifically, it does not increment the song record number. So, I added the key bit from what you did to get the named range column correctly into my code and it all works. Revised code is:
function addSong() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet();
var lastRow = sheet.getLastRow()
sheet.appendRow([lastRow+1]);
SpreadsheetApp.flush();
var range = sheet.getRange(sheet.getLastRow(), 1);
var songTitle = Browser.inputBox('New Song', 'Enter the song title', Browser.Buttons.OK_CANCEL);
var namedRange = sheet.getRange("Title");
var range=sheet.getRange(sheet.getLastRow(), namedRange.getColumn())
range.setValue(songTitle);
SpreadsheetApp.setActiveRange(range);
}
Try this:
function addSong() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet();
var resp=SpreadsheetApp.getUi().prompt('New Song', 'Enter the sone title', SpreadsheetApp.getUi().ButtonSet.OK_CANCEL);
var songTitle=resp.getResponseText();
var namedRange = ss.getRangeByName('Title');
var range=sh.getRange(sh.getLastRow()+1, namedRange.getColumn())
range.setValue(songTitle);
SpreadsheetApp.setActiveRange(range);
}
range.column documentation
How do I find the data in a column within an active row that is already defined in Google Apps Script?
I have an on edit function that sends an email when a column f is edited to a specific value. I need to find out the value in column b in order to include the value in the body of the message.
I have tried to use a range, but I am not understanding how i can define the second column for the active range that is already defined in the row variable. Here is what my code looks like. var called Values is the row of code I need help with. The row var is looking at the correct row. Any help is greatly appreciated.
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var sheetName = sheet.getName();
var cell = sheet.getActiveCell().getA1Notation();
var row = sheet.getActiveRange().getRow();
var cellvalue = sheet.getActiveCell().getValue().toString();
var values = row.getcell()[0,2].getvalue().toString();
var recipients = "kel...#trafficbuilders.us";
var message = '';
if(cellvalue === '✔'){
message = 'All Items have been received for .';
var subject = 'All Items have been received for ' ;
var body = message + ' Visit ' + ss.getUrl() + ' to view the changes';
MailApp.sendEmail(recipients, subject, body);
}
}
To fetch a certain data/value from a certain cell, here's what you can do:
First identify the cell where data is located using getRange(). There are several ways to use getRange.
Sample from the docs:
// Get a range A1:D4 on sheet titled "Invoices"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var range = ss.getRange("Sheet!A1:D4");
// Get cell A1 on the first sheet
var sheet = ss.getSheets()[0];
var cell = sheet.getRange("A1");
To actually fetch the content/data, use getValues() or getValue()
sample snippets:
// The code below gets the values for the range C2:G8
// in the active spreadsheet. Note that this is a JavaScript array.
var values = SpreadsheetApp.getActiveSheet().getRange(2, 3, 6, 4).getValues();
Logger.log(values[0][0]);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Passing only two arguments returns a "range" with a single cell.
var range = sheet.getRange(1, 1);
var values = range.getValues();
Logger.log(values[0][0]);