I have the below script that adds a timestamp when a new row of data is added.
function onEdit(e) {
var s = SpreadsheetApp.getActiveSheet();
if(s.getName() !== 'Sheet7' || e.range.columnStart != 1 || e.range.rowStart < 1) return;
e.range.offset(0, 1).setValue(e.value ? new Date() : "");
}
I tried the below for adding the checkbox, doesnt work.
function AddCheckBox() {
var cell = SpreadsheetApp.getActive().getDataRange('A1:B');
var criteria = SpreadsheetApp.DataValidationCriteria.CHECKBOX;
var rule = SpreadsheetApp.newDataValidation()
.requireCheckbox()
.build();
cell.setDataValidation(rule);
}
I would like it to also add a checkbox as well to the column next to where the timestamp appears.
Thanks in Advance
I just worked it out if anyones interested
function onEdit(e) {
var s = SpreadsheetApp.getActiveSheet();
if(s.getName() !== 'TEST' || e.range.columnStart != 1 || e.range.rowStart < 1)
return;
e.range.offset(0, 1).setValue(e.value ? new Date() : "");
var spreadsheet = SpreadsheetApp.getActive();
var cell = SpreadsheetApp.getActive().getRange('C1');
var criteria = SpreadsheetApp.DataValidationCriteria.CHECKBOX;
var rule = SpreadsheetApp.newDataValidation()
.requireCheckbox()
.build();
cell.setDataValidation(rule);
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('C1').activate();
spreadsheet.getCurrentCell()
.getNextDataCell(SpreadsheetApp.Direction.DOWN).activate();
spreadsheet.getActiveRange()
.autoFillToNeighbor(SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);
spreadsheet.getRange('A1').activate();
}
Related
Working in AppScript with Google Sheets and I have this script working to add notes based off of a cell value in a different column (selectedCell3 portion of the script). I'm wanting to add logic that essentially ADDS new text to the notes IF the cell value is changed. This is as far as I've gotten...
Still learning... be kind :)
//* Creates a Date Stamp if a column is edited.
//*/
//CORE VARIABLES
// The column you want to check if something is entered.
var COLUMNTOCHECK = 22;
// The SECOND column you want to check if something is entered.
var COLUMNTOCHECK2 = 18;
// The THIRD column you want to check if something is entered.
var COLUMNTOCHECK3 = 10;
// Where you want the date time stamp offset from the input location. [row, column]
var DATETIMELOCATION = [0,1];
// Where you want the SECOND date time stamp offset from the input location. [row, column]
var DATETIMELOCATION2 = [0,1];
// Where you want the THIRD date time stamp offset from the input location. [row, column]
var DATETIMELOCATION3 = [0,1];
// Sheet you are working on
var SHEETNAME = 'PM AUGUST 2022'
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
// var notesSheet = ss.getSheets()[0];
// var values_range = sheet.getRange("L2:L" + sheet.getLastRow());
// var notes_range = sheet.getRange("Z2:Z" + sheet.getLastRow());
// var notes = notes_range.getValues();
//checks that we're on the correct sheet.
if( sheet.getSheetName() == SHEETNAME ) {
var lastRow = sheet.getLastRow();
var selectedCell = ss.getActiveCell();
//checks the column to ensure it is on the one we want to cause the date to appear.
if( selectedCell.getColumn() == COLUMNTOCHECK && selectedCell.getValue() == "Billing") {
var dateTimeCell = selectedCell.offset(DATETIMELOCATION[0],DATETIMELOCATION[1]);
dateTimeCell.setValue(new Date());
}
var selectedCell2 = ss.getActiveCell();
if (selectedCell2.getColumn()==COLUMNTOCHECK2 && selectedCell2.getValue() !== ""){
var dateTimeCell2 = selectedCell2.offset(DATETIMELOCATION2[0],DATETIMELOCATION2[1]);
dateTimeCell2.setValue(new Date());
}
var selectedCell3 = ss.getActiveCell();
//var notesSheet = ss.getSheets()[0];
var values_range = sheet.getRange("J2:J" + lastRow);
var notes_range = sheet.getRange("L2:L" + lastRow);
var notes = notes_range.getValues();
var oldValue = notes.oldValue;
if (selectedCell3.getColumn()==COLUMNTOCHECK3 && selectedCell3.getValue() !== ""){
var dateTimeCell3 = selectedCell3.offset(DATETIMELOCATION3[0],DATETIMELOCATION3[1]);
dateTimeCell3.setValue(new Date());
if (oldValue != null) {
notes += 'Prior Entry :' + oldvalue;
}
values_range.setNotes(notes);
}
}
}
I think this is what you were shooting for
function onEdit(e) {
var sh = e.range.getSheet();
if (sh.getName() == 'PM AUGUST 2022') {
var lastRow = sh.getLastRow();
if (e.range.columnStart == 22 && e.value == "Billing") {
e.range.offset(0, 1).setValue(new Date());
}
if (e.range.columnStart == 18 && e.value !== "") {
e.range.offset(0, 1).setValue(new Date());
}
if (e.range.columnStart == 10 && e.value !== "") {
e.range.offset(0, 1).setValue(new Date());
let ts = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "yyyy MM dd HH:mm:ss")
sh.getRange(e.range.rowStart, 10).setNote(sh.getRange(e.range.rowStart, 10).getNote() + '\n' + sh.getRange(e.range.rowStart, 10).getValue() + ts);
}
}
}
Demo:
This question already has an answer here:
Google App Script onedit restrict to sheet
(1 answer)
Closed 8 months ago.
I have this script:
function onEdit2(e) {
const sheetName = "Charts"; // Please set your sheet name.
const dropDownCell = "B4:B6"; // Please set the range of dropdown list.
var spreadsheet = SpreadsheetApp.getActive();
var cell = spreadsheet.getActiveCell();
var cellR = cell.getRow();
var cellC = cell.getColumn();
var cellValue = cell.getValue();
if (cellR == 4 && cellC == 2) {
spreadsheet.getRange('B6').clearContent();
spreadsheet.getRange('B6').setValue('⚠️ SELECT');
}
}
I would like to make it work only on Charts tab without affecting any other tab. Right now script works in all tabs.
Actually the code doesn't make much sense to me. Here's what it boils down to:
function onEdit2(e) {
const sh = e.range.getSheet();
if(sh.getName() == "Charts" && e.range.columnStart == 2 && e.range.rowStart == 4) {
sh.getRange('B6').setValue('⚠️ SELECT');
}
}
function onEdit2(e) {
const sheetName = "Charts"; // Please set your sheet name.
if( e.range.getSheet().getName() === sheetName ) {
const dropDownCell = "B4:B6"; // Please set the range of dropdown list.
var spreadsheet = SpreadsheetApp.getActive();
var cell = spreadsheet.getActiveCell();
var cellR = cell.getRow();
var cellC = cell.getColumn();
var cellValue = cell.getValue();
if (cellR == 4 && cellC == 2) {
spreadsheet.getRange('B6').clearContent();
spreadsheet.getRange('B6').setValue('⚠️ SELECT');
}
}
}
I have the code below in my project, that helps me autofill the timestamp in "datetime" column in "test" sheet.
I want it to work for other sheets too. But I couldn't get it to work. Any help?
var SHEET_NAME = 'test';
var DATETIME_HEADER = 'datetime';
function getDatetimeCol(){
var headers = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(SHEET_NAME).getDataRange().getValues().shift();
var colindex = headers.indexOf(DATETIME_HEADER);
return colindex+1;
}
function onEdit() {
var ss = SpreadsheetApp.getActiveSheet();
var cell = ss.getActiveCell();
var datecell = ss.getRange(cell.getRowIndex(), getDatetimeCol());
if (ss.getName() == SHEET_NAME && cell.getColumn() == 1 && !cell.isBlank() && datecell.isBlank()) {
datecell.setValue(new Date()).setNumberFormat("yyyy-MM-dd hh:mm");
}
};
If you want to have an onEdit() trigger runs on multiple sheets use installable trigger.
Take your code and put it in a standalone script, not bounded to a sheet.
Then create function to setup onOpen Trigger
/**
* Creates a trigger for when a spreadsheet opens.
*/
function createSpreadsheetOnOpenTrigger() {
var id = 'YOUR_SHEET_ID';
var ss = SpreadsheetApp.openById(id);
ScriptApp.newTrigger('NAME_OF_FUNCTION_TO_RUN')
.forSpreadsheet(ss)
.onOpen()
.create();
}
reference : link
Then you just have to change id to setup trigger for all sheets you want the code run.
In the code take care to change to get infomration regarding celle and sheet from the event object :
function functionToRunOnEdit() {
var sheet = **e.range.getSheet()**;
var cell = **e.range**;
var name = sheet.getName();
var datecell = ss.getRange(cell.getRowIndex(), getDatetimeCol(sheet));
if (SHEET_NAMES.includes(name) && cell.getColumn() == 1 && !cell.isBlank() && datecell.isBlank()) {
datecell.setValue(new Date()).setNumberFormat("yyyy-MM-dd hh:mm");
}
};
Reference : link
I have also changed the variable name ss into sheet becausse it is a sheet but not a spreadsheet.
var SHEET_NAMES = ['test', 'test2'];
var DATETIME_HEADER = 'datetime';
function getDatetimeCol(sheet){
var headers = sheet.getDataRange().getValues().shift();
var colindex = headers.indexOf(DATETIME_HEADER);
return colindex+1;
}
function onEdit() {
var sheet = SpreadsheetApp.getActiveSheet();
var cell = sheet.getActiveCell();
var name = sheet.getName();
var datecell = ss.getRange(cell.getRowIndex(), getDatetimeCol(sheet));
if (SHEET_NAMES.includes(name) && cell.getColumn() == 1 && !cell.isBlank() && datecell.isBlank()) {
datecell.setValue(new Date()).setNumberFormat("yyyy-MM-dd hh:mm");
}
};
Below is a script that allows me to combine five different onEdit functions into a single function.
function onEdit(e){
if (e.range.getA1Notation() != "H12" || e.value != "submitResponse") return;
var sh = SpreadsheetApp.getActiveSpreadsheet();
var ss = sh.getActiveSheet();
var m = sh.getSheetByName("Master");
ss.getRange(2,1,ss.getLastRow(),3).copyTo(m.getRange(m.getLastRow()+1,1,ss.getLastRow(),3));
ss.getRange('H12').clearContent();
e.range.clearContents();
}
I'm now hoping to make this script more flexible. See Sheet1 of my spreadsheet: https://docs.google.com/spreadsheets/d/1EoOIQxWyKWOvtlCrmJNI76FAxGhzgXrE4s0F05tw2MY/edit#gid=0
It would be great if instead of limiting myself to H12, I could use the entire column of H:H to submit each corresponding row. So when I change H2 to submitResponse, it copies/pastes A2:G2 to the last row of the Master. When I change H3 to submitResponse, it copies/pastes A3:G3 to the last row of the Master. And so forth.
I tried my hand at this but no luck on execution. I guess I'm missing something.
function onEdit(e) {
var ss = e.source;
var s = ss.getActiveSheet();
var r = e.range;
var actionCol = 8;
var rowIndex = r.getRowIndex();
var colIndex = r.getColumnIndex();
var colNumber = s.getLastColumn()-1;
if (e.value == "submitResponse" && colIndex == actionCol) ;
var sourceRange = s.getRange(rowIndex, 1, 1, colNumber);
var targetRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Master").getRange(getLastRow()+1, 1, 1, colNumber);
sourceRange.copyTo(targetRange,SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
}
Try this:
function onEdit(e){
var sh=e.range.getSheet();
if (sh.getName()=='Sheet1' && e.range.columnStart==8 && e.range.rowStart>1 && e.value=="submitResponse") {
var msh=e.source.getSheetByName("Master");
msh.appendRow(sh.getRange(e.range.rowStart,1,1,7).getDisplayValues()[0]);
}
}
function onEdit(e){
var sh=e.range.getSheet();
if (sh.getName()=='Sheet1' && e.range.columnStart==8 && e.range.rowStart>1 && e.value=="Yes") {
e.range.setValue('');
var msh=e.source.getSheetByName("Master");
msh.appendRow(sh.getRange(e.range.rowStart,1,1,7).getDisplayValues()[0]);
}
}
So I have been working on a script to copy a formula down when a cell is edited to Yes in the column next to. This is working.
My question is I'd like to copy down the formula if there's a text or number in that a cell.
How can I add this my script.
I am fairly knew to this so any advice or documentation would be appreciated.
function onEdit(){
var sheetNameToWatch = "Request for Purchases";
var columnNumberToWatch = 10;
var valueToWatch="Yes";
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.getRange("K2").setFormula("=(c2*d2)");
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();
var val = sheet.getActiveCell().getValue();
if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && val==valueToWatch ) {
var targetCell = sheet.getRange(range.getRow(), range.getColumn()+1);
var d = ss.getRange("K2").copyTo(targetCell);
}
}
Is this what your trying to do?
function onEdit(e){
var sh=e.range.getSheet();
if(sh.getName()!='Request for Purchases') return;
if(e.range.columnStart==10 && e.value="Yes" ) {
e.range.offset(0,1).setFormula("=(C2*D2)");
}
}