Google Spreadsheet opening the last edited cell using script - google-apps-script

Using the following code allows the last edited cell to open when reopening a google spreadsheet.
function setTrigger() {
var ss = SpreadsheetApp.getActive();
ScriptApp.newTrigger("myFunction").forSpreadsheet(ss).onEdit().create();
}
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
var sName = sheet.getName();
var currentCell = sheet.getActiveCell().getA1Notation();
UserProperties.setProperty("mySheetName", sName);
UserProperties.setProperty("myCell", currentCell);
}
function onOpen() {
var lastModifiedSheet = UserProperties.getProperty("mySheetName");
var lastModifiedCell = UserProperties.getProperty("myCell");
SpreadsheetApp.getActiveSpreadsheet().getSheetByName(lastModifiedSheet).getRange(lastModifiedCell).activate();
}
I have a cell which has
=today()
Which updates so the code doesn't open the last row of my spreadsheet, but opens in the cell that updates with today().
How would I update the script to open the spreadsheet at my last edited cell in the last row of the spreadsheet?

Can you try this:
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
var sName = sheet.getName();
var currentCell = sheet.getActiveCell().getA1Notation();
var temp = "";
if (currentCell != "A1") {temp = currentCell;}
UserProperties.setProperty("mySheetName", sName);
UserProperties.setProperty("myCell", temp);
}
I added a temp variable and am doing a check (assuming only A1 has the today() function) before updating temp. Seems to work.

Related

Add value offset 1 from active cell together in spreadsheet script?

I want to to add value of cell B1 onto the existing value of A1. I would like the script to be relative, and leave the A1 cell as a pure value. Bonus if cell B1 is cleared at the end of the script.
function myFunction() {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getActiveSheet();
var cellRange = sheet.getActiveCell();
var selectedColumn = cellRange.getColumn();
var selectedRow = cellRange.getRow();
var CurrentCell = cellRange.getValue ();
var RightCell = getvalue.offset(0, 1);
Logger.log(`selectedColumn: ${selectedColumn}`);
Logger.log(`selectedRow: ${selectedRow}`);
Logger.log(`selected cell vale: ${cellRange.getValue()}`);
Logger.log(`selected cell vale: ${cellRange.getValue(0, 1)}`);
cellRange.setValue(CurrentCell+RightCell);
It leaves with me with the value of A1 plus 'Range'. If A1 is 7 it returns (7Range).
Trying to do this in Google App Scripts
Add value of cell one column to the right to current cell.
function myFunction() {
const ss = SpreadsheetApp.getActive();
const r = ss.getActiveCell();
r.setValue(r.offset(0,1).getValue() + r.getValue());
}
I get the same thing you do when I replace this with:
function myFunction() {
const ss = SpreadsheetApp.getActive();
const r = ss.getActiveCell();
r.setValue(r.offset(0,1).getValue() + r);
}
And I'm kind of surprised it doesn't return an error. But yeah it returns the class name

How do I run Google Sheets App Script on all sheets (tabs) in a Google Sheet?

I am trying to make the following script run on all the sheets(tabs) EXCEPT for one in my Google Sheet. I need some help. Thanks.
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
var cell = sheet.getRange("B4");
var refresh = parseInt(cell.getValue().toString());
var increment = refresh + 1;
cell.setValue(increment);
}
Assuming that your script gives you what you expect
All sheets :
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
ss.getSheets().forEach(function (sheet){
var cell = sheet.getRange("B4");
var refresh = parseInt(cell.getValue().toString());
var increment = refresh + 1;
cell.setValue(increment);
})
}
All sheets except a list (Edit by Cooper)
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
const exclA = ['Sheet1','Sheet2'];//excluded sheets
ss.getSheets().filter(sh => {!~exclA.indexOf(sh.getName())}).forEach(function (sheet){
var cell = sheet.getRange("B4");
var refresh = parseInt(cell.getValue().toString());
var increment = refresh + 1;
cell.setValue(increment);
})
}
bitwise not

Trigger a function when one cell is edited in Google Sheets

after some hours of reading documentation and other answers I couldn't solve my problem.
I'm trying to trigger a formula when one cell is edited. I get it to run when I just ask a value to be set (see commented line in the code) but not when I call another formula.
Here's the script:
function onEdit() {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getActiveSheet();
var cell = sheet.getActiveCell();
var row = cell.getRow();
var column = cell.getColumn();
if ( row ===1 && column ===4 && sheet.getName() ==="Hoja 1" ) {
sendData();
//sheet.getRange(1,1).setValue("5");
}
function sendData() {
var sheet = SpreadsheetApp.openById("1pxS9F9YEagzzRyTOqIDGVpDaCEiY_Z1DOYxq1QVzWGk");
var source_sheet = sheet.getSheetByName("Hoja 1");
var target_sheet = sheet.getSheetByName("Hoja 2");
var range1 = source_sheet.getRange(1, 7, 6, 1).getValues();
target_sheet.getRange(1,1,6,1).setValues(range1);
}
}
You have to pass the sheet into the called function this way:
sendData(spreadsheet);
function sendData(sheet) {
...
}
onEdit() is a simple trigger. Simple triggers cannot access services that require authorization. So it cannot call SpreadsheetApp.openById().
https://developers.google.com/apps-script/guides/triggers#restrictions
And pay attention -- in the function sendData() you're using the name sheet but it's a spreadsheet actually. This is not an error but it can be confusing a bit.

Generate new spreadsheet from a selected row based on template

With this script I can generate new tab from all the row in a spreadsheet:
function onOpen() {
SpreadsheetApp.getUi().createMenu('Tab Orari')
.addItem('Genera Tab Orari', 'createTabs')
.addToUi()}
function createTabs() {
var ss = SpreadsheetApp.getActive();
var templateSheet = ss.getSheetByName('xxx');
ss.getSheetByName('Generale').getRange('I2:I').getValues().filter(String)
.forEach(function (sn) {
if (!ss.getSheetByName(sn[0])) {
ss.insertSheet(sn[0], ss.getSheets().length, {template: templateSheet});
}
});
}
I would generate a new spreadsheet (and so not a tab), based on a template tab (in this case, the tab "xxx") only when I select a specific row and rename this Spreadsheet as the value in the cell in column I for that corresponding row.
How to do that?
Instead of insertSheet use copyTo(spreadsheet)
The spreadsheet name can be obtained by obtaining the row number of the selected cell and finding the entry in column 9 ("I") of this row.
Sample:
function onOpen() {
SpreadsheetApp.getUi().createMenu('Tab Orari')
.addItem('Genera spreadsheet', 'createSpreadsheet')
.addToUi()}
function createSpreadsheet() {
var ss = SpreadsheetApp.getActive();
// the following line means that the function will search for the spreadsheet name in the active sheet, no matter which one it is
var sheet = ss.getActiveSheet();
//the selected row
var row = sheet.getActiveCell().getRow();
// column 9 corresponds to "I"
var name = sheet.getRange(row, 9).getValue();
var templateSheet = ss.getSheetByName('xxx');
var newSpreadsheet = SpreadsheetApp.create(name);
templateSheet.copyTo(newSpreadsheet);
}

Google Spreadsheet onEdit - limit to a range

I would like to fire a trigger with onEdit if a event (edit a cell) happens in a specific range of a Sheet (NOT all Sheet)?
Exemple: I only want to trigger if a cell of the range called "trgRng" (C10:C250) of Sheet_1 is edited.
I coded as below, but it trigger all sheets not only a specific range of a specific sheet
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
// source sheet
var sSh = ss.getSheetByName("Diario");
var aCell = sSh.getActiveCell().getValue();
// destiny sheet - set value from source sheet
var dSh = ss.getSheetByName("Auxiliar");
dSh.getRange('L4').setValue(aCell);
}
Is it possible? How?
I already solve it:
{
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
// source sheet
var sSh = e.source.getActiveSheet();
var maxRow = sSh.getMaxRows();
var name = sSh.getName();
var rng = e.range;
var rngCol = rng.getColumn();
if (sSh.getName() == "Diário" && rng.getColumn() == 8) {
ss.getSheetByName("Diário");
var aCell = sSh.getActiveCell().getValue();
// target sheet
var shD = ss.getSheetByName("Auxiliar");
shD.getRange('L4').setValue(aCell);
}
}
}