I'm trying to use HTTP Get in Google Spreadsheet, to be able to create a URL to a specific cell in a sheet. I've been searching for how to do it, and I've tried using this way (the GET part), for example.
Here's the code I've put together:
function doGet(e){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Kunder");
var row = e.parameter["row"];
var col = e.parameter["col"];
var ss = SpreadsheetApp.getActiveSpreadsheet();
var range = sheet.getRange(row, col);
SpreadsheetApp.setActiveSheet(sheet);
SpreadsheetApp.setActiveRange(range);
}
And this is an example URL:
https://docs.google.com/spreadsheets/d/[spreadsheet ID]/edit?row=5&col=1
When I run the script I get this error:
TypeError: Cannot read property "parameter" from undefined.
I'm I doing something wrong? Or isn't it possible?
Thanks in advance,Oskar
EDIT
This is my current code:
function doGet(e){
var ss = SpreadsheetApp.openById("[id]");
SpreadsheetApp.setActiveSpreadsheet(ss);
var sheet = ss.getSheetByName("Kunder");
var row = Number(e.parameter.row);
var col = Number(e.parameter.col);
var range = sheet.getRange(row, col);
SpreadsheetApp.setActiveSheet(sheet);
SpreadsheetApp.setActiveRange(range);
}
The code runs but finishes with
The script completed but did not return anything.
I want to open the spreadsheet and then set the active range from the GET parameters.
The url you show is not valid.
To use that kind of script you have to deploy the script as a webapp and use the resulting .exec url with parameters.
At the end it will look like this :
https://script.google.com/macros/s/AKfycbygwUwXXXXXXXXxJcKpBvsbflmnZ0Uqfu-JISWTZNvar32s3v_hl/exec?row=5&col=1
EDIT : there will be no activeSheet in that context, use openById or openByUrl instead.
Also the values you get for row and columns are strings, you should make them integers and use a normal sheet.getRange(row,col) to select a range.
EDIT2 :
this url :
https://script.google.com/macros/s/AKfycbwZbGW6E483BUplvLjCjNCXKjjiRorqzR9lruSydogeuU-YIvID/exec?row=1&col=1
and this code :
function doGet(e){
var ss = SpreadsheetApp.openById("1sbJXuEpL_88-u-Bm4QOCAM3SVddFwZKI0c1kxeRpcos");
var sheet = ss.getSheetByName("Sheet1");
var row = Number(e.parameter.row);
var col = Number(e.parameter.col);
var range = sheet.getRange(row, col);
sheet.getRange(row, col).setValue('test ok')
}
writes in this sheet : see cell A1 or test on another row and column.
You'll indeed receive a message that nothing was returned and this is actually true ! we don't return anything in that code, this is just a demo ;-)
EDIT 3 : as mentioned in the comments, to get something returned by this function you have to resturn something...
Here is an example :
function doGet(e){
var ss = SpreadsheetApp.openById("1sbJXuEpL_88-u-Bm4QOCAM3SVddFwZKI0c1kxeRpcos");
var sheet = ss.getSheetByName("Sheet1");
var row = Number(e.parameter.row);
var col = Number(e.parameter.col);
var range = sheet.getRange(row, col);
var value = sheet.getRange(row, col).getValue();
sheet.getRange(row, col).setValue('test ok');
var stringValue = value==''?'empty':value;
Logger.log(stringValue);
return ContentService.createTextOutput(stringValue).setMimeType(ContentService.MimeType.TEXT);
}
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 am new with javascript and I am trying to do a script that copy the value of the last raw of a sheet to the last raw of another sheet, if I don´t set the number of columns to the range it only copy one value, however if I set the amount of columns it only returns that copy to is not a function, please if you can help me.
This is the code I am using:
function ORderReservation() {
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var hOrigen = spreadsheet.getSheetByName('Form responses 1');
var hDestino = spreadsheet.getSheetByName('TODAS AGENCIAS');
var filActiva = hOrigen.getLastRow();
var activeCell =hOrigen.getRange(filActiva,1)
var valor = activeCell.getValue();
if( valor>= 0)
var rngOrgn = hOrigen.getRange(filActiva,1,1,8).activate;
var detsin = hDestino.getLastRow()+1;
rngOrgn.copyTo(hDestino.getRange(hDestino.getLastRow()+1,1,1,8),SpreadsheetApp.CopyPasteType.PASTE_VALUES,false);
}
I tried to use the following code to replace the current cell formula with test text instead.
function replace_content() {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getActiveSheet();
var cellRange = sheet.getActiveCell();
cellRange.setValue("test")
return true
)
But something goes wrong and I get this error msg
Error
Exception: You do not have permission to call setValue (line 59).
Please advice.
This function runs for me with no problems:
function replace_content() {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getActiveSheet();
var cellRange = sheet.getActiveCell();
cellRange.setValue("test")
return true;
}
I have tried 10+ variations of the following script to try and achieve what I thought would be a simple function to copy the values in the last row of my Google Sheet and paste them into the penultimate row. The last and penultimate rows that contain data will always be changing so I cannot specify a static range for either. Here are the 2 closest versions I've written:
function Export() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var database = SpreadsheetApp.openById("xxx");
var source = ss.getSheetByName('aaa');
var last_row = source.getLastRow();
var second_last_row = (last_row-1);
var copyData = source.getRange(second_last_row).setValues(last_row);
}
The error here is "Range not found" for the 'var CopyData' line. If I re-write this line as follows:
var copyData = source.getRange.second_last_row.setValues(last_row);
I get an error that says "cannot read property 'setValues' of undefined".
I have tried this version as well:
function copyv() {
var target = SpreadsheetApp.openById("xxx");
var target_sheet = target.getSheetByName("aaa");
var last_row = target_sheet.getLastRow();
let source = last_row
let destination = last_row-1
source.copyTo(destination, SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
}
And in this sample I get the error that "source.copyTo is not a function" although this works in other functions I've written where the range is static (ex: A5:BC, not "last_row").
Any and all help is very appreciated.
You have to grab values not just the row:
function Export() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var database = SpreadsheetApp.openById("xxx");
var source = ss.getSheetByName('aaa');
var last_row = source.getLastRow();
var last_column = source.getLastColumn();
var data = source.getRange(last_row,1,1,last_column).getValues();//gets actual data
var destination_range = source.getRange(last_row-1,1,1,last_column);//creates target range
source.getRange(data).setValues(destination_range);
}
In your version you were trying to copy an integer to an integer rather than an array to a range.
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]);