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);
}
}
}
Related
I'm trying to.. 1) replace a substring from the contents of the active cell in my sheet, and.. 2) move the active cell down one spot, and repeat until the active cell value is empty. Here's my function, but nothing happens when I run it...can anyone see why?
var app = SpreadsheetApp;
var mySheet = ss.getSheetByName('NAME');
var activeCell = mySheet.getActiveCell();
var cellValue = activeCell.getValue();
var activeRow = activeCell.getRow();
function replaceStringInCell(){
while(cellValue != ''){
var newCellValue = cellValue.replace('MC/Visa/Discover', 'CC');
activeCell.setValue(newCellValue);
activeRow++;
mySheet.getRange(activeRow, 7).activate();
}
}
Issue:
Unnecessary looping over each cell
Solution:
Use batch operations
Sample Script:
function replaceColA() {
var ss = SpreadsheetApp.getActive();
var mySheet = ss.getSheetByName('NAME');
var cellRange = mySheet.getRange('A1:A' + mySheet.getLastRow());
var cellValues = cellRange.getValues().map(replaceStringInCell); //call replace function on all values in range
cellRange.setValues(cellValues); //set mapped values back to range
}
function replaceStringInCell(cellValue) {
if (cellValue.map) {
//if cellValue is a array
return cellValue.map(replaceStringInCell); //recurse
} else {
return cellValue.replace('MC/Visa/Discover', 'CC');
}
}
To Read:
Arrays
Array#Methods
Array#2DFromSpreadsheets
Array#map
String#replace
Self taught noob, working in google sheets, sorry in advance for the mess here. I am trying to pull data from two cells into a new sheet. Both of these are pulled in after I trigger them with an onEdit. I need to enter a value and hit enter on both cells for them to be placed into the new sheet. How would I set this up so that when I trigger "D9" I would pull in "C9" plus "D9"?
function onEdit(e){
// Set a comment on the edited cell to indicate when it was changed.
if(e.range.getA1Notation() === 'C9'){
var input = e.range.getValue();
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var inputSheet = sheet.getSheetByName("input")
var masterSheet = sheet.getSheetByName("master inventory");
var currentRow = inputSheet.getRange("E9").getValue();
masterSheet.getRange("A3:A1000").getCell(currentRow, 1).setValue(input);
inputSheet.getRange("E9").setValue(currentRow, 1);
}
if(e.range.getA1Notation() === 'D9'){
var input = e.range.getValue();
e.range.clearContent();
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var inputSheet = sheet.getSheetByName("input")
var masterSheet = sheet.getSheetByName("master inventory");
var currentRow = inputSheet.getRange("E9").getValue();
masterSheet.getRange("B3:B1000").getCell(currentRow, 1).setValue(input);
inputSheet.getRange("E9").setValue(currentRow + 1);
}
}
I am not sure exactly what you are trying to do, but try this:
function onEdit(e) {
var x=e.source.getSheetName()
if( x == "input" ) { //checks that we're on the correct sheet
var y = e.range.getSheet().getActiveCell();
var z= e.range.getSheet().getActiveCell().getA1Notation()
var row=e.range.getSheet().getActiveCell().getRow()
if(z=="D9" && x=="input"){
var cell=e.range.getSheet().getActiveCell().getValue()
e.range.getSheet().getActiveCell().clearContent()
var nextCell =e.range.getSheet().getActiveCell().offset(0,-1).getValue()
e.range.getSheet().getActiveCell().offset(0,-1).clearContent()
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var masterSheet = sheet.getSheetByName("master inventory");
masterSheet.getRange("A3:A1000").getCell(row-2, 1).setValue(nextCell);
masterSheet.getRange("B3:B1000").getCell(row-2, 1).setValue(cell);
}}}
I have a source cell with the range name "source".
I have some target (non-adjacent) cells with the range names "target1", "target2", "target3".
If the cell "source" is changed, I want the value of it to be copied into the 3 target cells.
I'm stumbling at the first hurdle in that I cannot work out how to test if I'm editing the "source" cell.
All the documentation seems to be about naming ranges rather than then working with them.
So far I have...
function onEdit()
{
var sh = SpreadsheetApp.getActiveSheet();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var range = ss.getActiveRange();
var C = range.getColumnIndex();
var a = sh.getActiveCell();
if( range == "LXbar1" )
{
SpreadsheetApp.getUi().alert('You are HERE!');
}
}
revised code:
function onEdit(e)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
// Active range
var eventRange = e.range;
// Active range address
var eventRangeA1 = eventRange.getSheet().getName() + "!" + eventRange.getA1Notation();
// Comparison range name
var sourceRange = ss.getRangeByName("rLXbar1");
// Comparison range address
var sourceRangeA1 = sourceRange.getSheet().getName() + "!" + sourceRange.getA1Notation();
if (eventRangeA1 === sourceRangeA1)
{
// Target range name
var targetRange = ss.getRangeByName("rLamp08");
targetRange.setValue(sourceRangeA1.value);
}
}
Don't use Active elements in onEdit triggers, use the built in event object it will ensure it will always use the actually selected cell when the edit happened.
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var eventRange = e.range;
var sourceRange = ss.getRangeByName("source");
var eventRangeA1 = eventRange.getSheet().getName() + "!" + eventRange.getA1Notation();
var sourceRangeA1 = sourceRange.getSheet().getName() + "!" + sourceRange.getA1Notation();
if (eventRangeA1 === sourceRangeA1) {
SpreadsheetApp.getUi().alert("You are HERE!");
}
};
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.
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.