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!");
}
};
Related
I want to create a button that moves a given number of cells from one sheet into another sheet. The starting range cell is always the same (A9) and there will be always 8 columns in total. The final row will depend on how many rows the user completes. How can I indicate this behaviour in my range code?
I think this needs to be instructed in range field, but couldn't obtain what I need.
function finishReport() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
sheet = ss.getActiveSheet(),
sheetName = sheet.getName(),
data = sheet.getDataRange().getValues();
if (sheetName == "Main") {
var range = sheet.getActiveRange(),
startRow = range.getRowIndex(),
numRows = range.getNumRows(),
numCols = range.getNumColumns()
if (numCols == 8 {
if (data.length > 1) {
var values = range.getValues(),
nextSheet = ss.getSheetByName("History record")
lastRow = nextSheet.getLastRow();
nextSheet.getRange(lastRow+1,1,numRows,8).setValues(values);
sheet.deleteRows(startRow,numRows);
}
}
}
}
I expect that all rows entered by user in tab Main are moved to tab History record. Any thoughts?
Query
You want to copy a range starting at cell A9 and stretching over 8 columns until the last row containing contents
Problem
You are using getActiveRange() which returns you only the selected (=highlighted) range - if no range is highlighted in the sheet, only the active cell will be returned as a range
Solution
Select the range manually by retrieving the last content containing row and column with
mainLastRow = sheet.getLastRow();
mainLastColumn = sheet.getLastColumn();
and defining
var numRows = mainLastRow-startRow+1;
var numCols = mainLastColumn-startColumn+1;
Full Code
function finishReport() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
sheet = ss.getActiveSheet();
sheetName = sheet.getName();
if (sheetName == "Main") {
var startRow = 9;
var startColumn=1;
var mainLastRow = sheet.getLastRow();
var mainLastColumn = sheet.getLastColumn();
var numRows = mainLastRow-startRow+1;
var numCols = mainLastColumn-startColumn+1;
var range = sheet.getRange(startRow,startColumn,numRows,numCols);
Logger.log(numRows);
Logger.log(numCols);
Logger.log(mainLastRow);
Logger.log(mainLastColumn);
if (numCols == 8) {
if (numRows >= 1) {
var values = range.getValues();
nextSheet = ss.getSheetByName("History record");
lastRow = nextSheet.getLastRow();
nextSheet.getRange(lastRow+1,1,numRows,8).setValues(values);
sheet.deleteRows(startRow,numRows);
}
}
}
}
Annotation
You could also retrieve the range with getDataRange(), but you would have to correct manually for the fact that dataRange starts by definition in cell A1.
I hope this works for you.
function onOpen(e) {
// Add a custom menu to the spreadsheet.
SpreadsheetApp.getUi()
.createMenu('Report')
.addItem('Finish', 'FinishReport')
.addToUi();
}
function FinishReport(){
var sp = SpreadsheetApp.getActiveSpreadsheet();
var sh = sp.getActiveSheet();
var as =sp.getSheetByName('Main').activate();
var MainRange = sp.getRange('A9:H9').activate();
var MainLastRow = sp.getLastRow();
var selection = sp.getRange('A9:H' + MainLastRow).activate();
sp.getCurrentCell().activateAsCurrentCell();
sp.setActiveSheet(sp.getSheetByName('History record'), true);
var lastRow = sp.getLastRow();
var newRow = lastRow+1;
sp.getRange('A'+newRow+':H'+newRow).activate();
sp.getCurrentCell().activateAsCurrentCell();
sp.getRange('Main!' + selection.getA1Notation()).moveTo(sp.getActiveRange());
}
REFERENCE
Spreadsheet Custom Menu
Apps Script, SpreadsheetApp
This question is related to Google-Sheets App Script
I am trying to compare two values from the source sheet to two values in
target sheet respectively then based on the row number found in the
target sheet I want to change the font colour of this cell to that of
the font-clour of the cell found in column-E of the target sheet.
My Logic is shown below.
SrcSheet = Batches-sheet
TargetSheet = Activity-sheet
if srcDate is found in the targetDateRange in TargetSheet AND if
srcBatchName is found in the targetBatchNameRange in TargetSheet THEN
set the font-colour of this cell (srcDate in srcSheet) to the
fontcolour of targetLessonCell of in TargetSheet
My Function definition and Function Call looks like this;
Function Call:
=updateBatchActivity(W18,Activity!B1:B700,I18,Activity!E1:E700)
Function Definition:
function updateBatchActivity() {
var app = SpreadsheetApp;
var sourceSheet = app.getActiveSpreadsheet().getSheetByName("Batches");
var targetSheet = app.getActiveSpreadsheet().getSheetByName("Activity");
for (var srcRow=3; srcRow<=8; srcRow++) {
srcBatch = sourceSheet.getRange(srcRow,9).getValue()
Logger.log('Row ' + srcRow + ' BatchName: ' + srcBatch);
for (var srcCol=21;srcCol<=36; srcCol++) {
var range = sourceSheet.getRange(srcRow,srcCol);
srcDate = range.getValue()
for (var dstRow=2; dstRow<=10; dstRow++) {
var targetDate = targetSheet.getRange(dstRow,2).getValue();
if (srcDate == targetDate) {
var targetBatch = targetSheet.getRange(dstRow,5).getValue();
if (srcBatch == targetBatch) {
var fontColour = targetSheet.getRange(dstRow,6).getFontColour();
range.setFontColor(fontColour);
}
}
}
}
}
}
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 need a script to automatically add a blank line at the beginning whenever I finish filling the field in column 8 of a line?
Note: It is for this new line preserve the formula of the previous line.
This is the script that I am using.
It works by writing any part of the line (any column)
but I wanted him to be activated only when i write something in column 8.
function onEdit(e) {
if (e) {
var ss = e.source.getActiveSheet();
var r = e.source.getActiveRange();
// If you want to be specific
// do not work in first row
// do not work in other sheets except "Outubro"
if (r.getRow() != 1 && ss.getName() == "Outubro") {
// E.g. status column is
status = ss.getRange(r.getRow(), 8).getValue();
// Status
if (status == 'yes', 'null') {
var planilha = SpreadsheetApp.getActiveSpreadsheet();
var folha = planilha.getActiveSheet();
if (folha.getName() === "Outubro")
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
sheet.insertRowBefore(4);
var rangeToCopy = sheet.getRange(3, 1, 1, sheet.getMaxColumns());
rangeToCopy.copyTo(sheet.getRange(4, 1));
}
}
}
}
Thank you for the help from the community.
The code is now working.
I'll leave the code here for anyone who need.
Once again thank you all for the help.
function onEdit(e) {
if (e) {
var ss, activeRange, activeColumn;
var planilha = SpreadsheetApp.getActiveSpreadsheet();
var folha = planilha.getActiveSheet();
if (folha.getName() === "Outubro")
ss = SpreadsheetApp.getActiveSpreadsheet();
activeRange = ss.getActiveRange();
activeColumn = activeRange.getColumn();
//Logger.log('activeColumn: ' + activeColumn); //VIEW LOGS
if (activeColumn !== 8) {return;};//Quit if not column 8 {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
sheet.insertRowBefore(4);
var rangeToCopy = sheet.getRange(3, 1, 1, sheet.getMaxColumns());
rangeToCopy.copyTo(sheet.getRange(4, 1));
}
}
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);
}
}
}