Add note based on value of another cell - google-apps-script

For all I researched, I know there is not an integrated funcion that can do this, but I know it can be done with scripts. I want a script to create note in one specific cell based on another specific cell value.
example in the sheet I'll share
https://docs.google.com/spreadsheets/d/1I7ictcN_tEQyZXFljAVFm1VqZzafIezF1xR5AYW5pVc/edit?usp=sharing
I want the note on D12 to reflect any changes in the value of A8 for example.
Best case would be, if I could use this script as a function calling whatever cell I wanted.
I saw some examples using onEdit as trigger but I don't know if that would work because the cell in question wouldnt even be touched.
I also saw some exemples using get range to target the cells but then I would have to make a new script for each iteration.

Recording User Edits in A8 in the Notes of D12
function onEdit(e) {
const sh = e.range.getSheet();
if(sh.getName() == "Sheet name" && e.range.columnStart == 1 && e.range.rowStart == 8) {
sh.getRange(12,4).setNote(sh.getRange(12,4).getNote() + '\n' + e.value);
}
}

Related

Keep column locked from editing unless the input in the corespodnig one was entered

Dear community I'm reaching out to you as I couldn't find any relevant answer to my question in hopes that you will be able to help.
Basically, my case is relatively simple, I have column A and Column B in the specific spreadsheet name XYZ. In column A I have a drop-down list, with the predefined input to chose from. In column B the user is ought to enter a date. The problem is users oftentimes select the value from the dropdown list without entering a corresponding date value. I tried to use data validation - "custom formula is", to lock column A from editing unless the value is entered in column B unfortunately, it overwrites the dropdown list functionality.
So I wonder if there is a way to translate this formula to apps script to be true for any other input value from the dropdown list than "vacant".
For the sake of the argument, let's say the range in column A is from A15:A95 and B is from B15:B95.
I look forward to hearing from you guys!
Force insertion of date with Modal Dialog
Two functions, both server side.
function onMyEdit(e) {
e.source.toast('Entry');
const sh = e.range.getSheet();
if(sh.getName() == 'Sheet0' && e.range.columnStart == 1 && e.range.rowStart > 1 && e.value) {
e.range.offset(0,1).activate();
const html = '<input type="date" id="d1" onChange="google.script.run.withSuccessHandler(_ => {google.script.host.close();} ).insert(this.value.toLocaleString());" />';
SpreadsheetApp.getUi().showModalDialog(HtmlService.createHtmlOutput(html),"Enter Date")
}
}
function insert(v) {
SpreadsheetApp.getCurrentCell().setValue(v)
}
Demo:
Must be a installable onEdit function. Please do not name it onEdit(e)

How to update the timstamp while choosing the same item in drop-down list in google sheet?

Hi everyone,
I have a script where it is able to print the current timestamp in Cell C8 when I choose Transfer in cell B8. However, when I click Transfer in the drop-down list for the second time, the timestamp will not update since the word is still the same. My code is is attached below:
function onEdit(e){
const dt = Utilities.formatDate(new Date(), 'Asia/Singapore', 'HH:mm:ss');
if (e.range.columnStart == 2 && e.range.rowStart >= 8) {
if (e.value == 'Transfer') {
e.range.offset(0, 1).setValue(dt);
} else {
e.range.offset(0, 1).setValue('');
}
}
}
I understand that cell B8 need to change so that OnEdit function will change and print the current timestamp. I also know that I can include Transfer 1, Transfer 2 in my drop-down list and my script but this is not an ideal way. Is there any way to change the timestamp even though I'm still choosing Transfer in the drop-down list? Possible other type of function instead of OnEdit?
Any help will be greatly appreciated!
Why not just add a clear of B8 before the }else{ in the script?
i.e., after:
e.range.offset(0, 1).setValue(dt);
... add this line (indented to the same level as the above line for clarity)...
e.range.clearContent();

.getUsername() not functioning properly within Workspace account

A two part question:
The script below is intended to watch for edits in column 6 of a specific sheet and when triggered, execute the three parts of the script (marked A-C below). All works properly for me as the owner of the sheet within a Workspace account. For other users who edit the sheet, part A works properly, but not B or C. All users are part of the same Workspace account.
As an alternative to watching just Col 6, any suggestions as to how I could rewrite the script to record the location of the most recently edited cell on a given row?
Thank you.
function onEdit(e) {
if (
e.source.getSheetName() == "STATUS" &&
e.range.columnStart == 6 &&
e.range.columnEnd == 6 &&
e.range.rowStart >= 2 &&
e.range.rowEnd <= 2000
) {
var range = e.range;
var status = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("STATUS");
//A: on modified row, copy live status to permanent
status.getRange(range.getRow() , 11).copyTo(status.getRange(range.getRow() , 7), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
//B: set time & date modified
status.getRange(range.getRow() , 8).setValue(new Date());
//C: set user who last modified ID column
status.getRange(range.getRow() , 9).setValue(Session.getEffectiveUser().getUsername());
}
else
{ return; }
}
With getEffectiveUser() you get the user under whose authority the script is running, change this to getActiveUser(). See the documentation. To get the column & row of the onEdit range try this:
e.range.getRow()
e.range.getColumn()
In a simple onEdit() trigger, Session.getEffectiveUser() is equivalent to Session.getActiveUser(), which states:
The circumstances in which the email address is available vary: for example, the user's email address is not available in any context that allows a script to run without that user's authorization, like a simple onOpen(e) or onEdit(e) trigger...
You should be able to generalize "email address" to username or any other personally identifiable information. That kind of information requires authorization.
The only way to make this work would be to have your other users authorize this script first.
If you don't want the trigger to be restricted to edits in Column 6, remove that condition from your if statement.
// These lines specify column 6
e.range.columnStart == 6 &&
e.range.columnEnd == 6 &&

Date stamp script not working with arrayformula

I'm trying to use a simple date stamp script in Google Sheets that will input today's date upon edit of another column but then will not change thereafter. I also have an array formula set on the column the script is looking at for the edits and it appears the array formula is preventing the script from working properly. Here is the formula:
={"Triggered";arrayformula(IFS(E2:E="","",C2:C>=E2:E,"✅",TRUE,""))}
And this is the script:
function onEdit(e) {
var sh = e.source.getActiveSheet();
if(sh.getName() !== "Watchlist" || e.range.columnStart !== 8) return;// 1 is A, 2 is B, 3 is C, etc
var o = e.range.offset(0, 1)
if(!o.getValue()) o.setValue(new Date())
}
Any help with what I'm doing wrong is appreciated. Thanks.
Unfortunately, according to the official documentation :
An installable edit trigger runs when a user modifies a value in a
spreadsheet.
Even when the output of the arrayformula is updated, the cell still contains the same formula. Therefore, the onEdit trigger won't fire since there wasn't any edit action by the user.
Read also the discussion here.

delete entire row if any cell is empty

Just like the title says. I found a solution if one column is blank, but not if any are blank.
I crafted this with the help of another post, though I don't have a clue re: google scripts.
function onEdit(e) {
//Logger.log(JSON.stringify(e));
//{"source":{},"range":{"rowStart":1,"rowEnd":1,"columnEnd":1,"columnStart":1},"value":"1","user":{"email":"","nickname":""},"authMode":{}}
try {
var ss = e.source; // Just pull the spreadsheet object from the one already being passed to onEdit
var s = ss.getActiveSheet();
// Conditions are by sheet and a single cell in a certain column
if (s.getName() == 'Sheet1' && // change to your own
e.range.columnStart == 1 && e.range.columnEnd == 99 && // only look at edits happening in col C which is 3
e.range.rowStart == e.range.rowEnd ) { // only look at single row edits which will equal a single cell
checkCellValue(e);
}
} catch (error) { Logger.log(error); }
};
function checkCellValue(e) {
if ( !e.value || e.value == 0) { // Delete if value is zero or empty
e.source.getActiveSheet().deleteRow(e.range.rowStart);
}
}
problem is, I have no idea how to actually "use" it
Demo of the setup.
e.range.columnStart and e.range.columnEnd need to be equal to each other in this case
In the spreadsheet that you'd like to use this in, click on Script Editor in the Tools menu. Select Blank Project in the dialog that comes up, and then replace the code in the window with the code you've posted above. Save and name your project, then click Current Project's Triggers in the Resources menu. Click the link to add a new trigger. You'll see a series of drop down boxes, select the function (onEdit()), From Spreadsheet (the event source), and "On Edit" (the event). You have the option to set failure notifications. Click save to save the trigger. You'll be prompted to provide authorization, once you do, the script should run as expected.