Is it possible to set Notes with automatically inserted values using google sheets? - google-apps-script

I was trying to figure out a way to insert Notes automatically, when i enter something in a specific cell. This thread was suggested to me and tried it out: https://support.google.com/docs/thread/13317657?hl=en. I changed the function mentioned a littlebit, to fit my project:
function onEdit(e) {
if(e.source.getActiveSheet().getName() === 'Testsheet') {
if(e.range.columnStart === 8 && (e.range.rowStart > 12 && e.range.rowStart < 21)) {
return e.range.offset(0, -6).setNote(e.value);
}
}
}
This way when I enter values in the range H13:H20, the same values get inserted as Notes on the corresponding (0, -6) offset cells. Which is a step in the right direction, but not yet what I'm truly trying to achieve. The problem is, this only works, when the values are entered manually. But for my project, I need the notes also to be set, when values are filled in automatically, using a function that fills in certain information in a row, depending on the selected dropdown choice.
In my sheet for example:
=if(B17="Test1",Testsheet!C10:Testsheet!H10)
When the Information is filled in that way, it doesn't give me the Note in the offset cell. Is there a way, so that it does?
Here is a link to my Testsheet, where you can see what I'm trying to do : https://docs.google.com/spreadsheets/d/16c2hVDM_FTnHWerKjnOauCopwVZSnKCwAMVi6-Z8vBo/edit?usp=sharing
Thanks for any tips and help in advance!

As you can see in the documentation:
Script executions and API requests do not cause triggers to run. For
example, calling Range.setValue() to edit a cell does not cause the
spreadsheet's onEdit trigger to run.
The only option would be to directly run the notes function with the values as parameter instead of inserting the "row 16" values, as updating the sheet by a script or formula won't trigger the function onEdit().

Related

Google Sheets Script to Return Specific Text Based on Specific Text from Adjacent Cell

first post, thanks in advance for any help.
I have a fair amount of experience creating spreadsheets with included formulas, but I'm a total newb when it comes to Google Scripts. I'm assuming that what I'm trying to accomplish is relatively common and simple, but I can't find the answer.
All I want to do is have the cells column B return specific text if a specific text is entered into the adjacent cells in column A.
If that specific text is NOT entered into column A, then column B should remain (or go back to being) freely editable. This would need to apply to the entire column(s)/ranges.
I'm pretty sure this would involve the onEdit trigger, but I have never used Google Scripts and I don't know how to write that script. Would anyone would be so kind as to assist me with this?
Thanks!
An onEdit function Based upon what you described
function onEdit(e) {
const sh = e.range.getSheet();
if(sh.getName() == "Sheet Name" && e.range.columStart == 1 && e.value == "Specific Text") {
e.range.offset(0,1).setValue('Specific Text');
}
}
You need to update Sheet Name and Specific Text. You cannot run this type of function without supplying the event object which normally comes from the onEdit trigger. So this function cannot be from the script editor or a menu.

Apps Script: How to capture oldValue of a cell after a change made by sheets api?

I am running a python script that updates my google sheet (named 'Notifications') through the sheets api every 5 minutes. The python script first clears and resizes the sheet to only two rows and then adds the new data on top and the sheet's size gets automatically expanded. I've done that not to have empty rows if my new data is smaller in size than the old one.
So the sheet is updated every 5 minutes but if nothing new happened in my data source, the new values are going to be the same as the old. In particular, the value in 'A2' is going to be the same as before the update. If something new has happened, the value in 'A2' is going to be different. I want to track that and get a pop-up message that notifies me that something new has happened after the update.
So as far as I understand I have to use onChange because onEdit only works if the changes are made by a human and cannot track changes made by the api. I'm uncertain how to do that though.
With onEdit I've achieved it and get a pop-up if I modify the content of 'A2' manually but nothing happens when python updates my sheet.
Here's the code with onEdit:
function onEdit(e) {
if (e.range.getA1Notation() === 'A2') {
let oldValue = e.oldValue
let newValue = e.newValue
if (oldValue != newValue) {
Browser.msgBox('The value in A2 changed from ' + oldValue + ' to ' + newValue);
}
}
}
So the end goal is to get a pop-up not every time python updates my sheet, but only if there's a change in the value of 'A2' after the update.
How can I do this?
The only way to get a pop-up when a a change is made through an API is by using polling function from client-side code. One way to do this is by using a sidebar to hold the client-side code.
You should store somewhere the old values then compare with the current values.
Related
How do I make a Sidebar display values from cells?
How to get all old values for a mulitple cell range with onEdit trigger
onEdit(e) not generating trigger event when cell value changes due to inbuilt function
You can use onChange trigger with a "ValueInputOption"="USER_ENTERED" to trigger a function when a script edits a sheet. But you can't show anything in the UI from the triggered context so getting a pop up will be impossible.

How do I create a function that returns the last time a cell was modified?

I'm looking to write a function for Google Sheets Script that accepts a cell reference and outputs the date that cell was last modified. I'm not sure how to get started.
There's no direct approach to get the exact date of a cell's edit.
Method 1
Retrieve the modifiedTime using the Drive API's Files:get which will return, according to the documentation:
The last time the file was modified by anyone (RFC 3339 date-time).
Method 2
What you can do instead is to make use of the onEdit(e) trigger and monitor each change from the particular cell.
So for example, considering that the cell you want to check is A1, you can use this snippet:
Snippet
function onEdit(e) {
if (e.range.getRow() == 1 && e.range.getColumn() == 1) {
var date = new Date();
console.log(date);
}
}
Explanation
The above function is an onEdit trigger which will fire every time there's an edit made on the sheet. Since you want to check this for a particular cell, the event object e has been used in order to get the row and the column of the cell which has been edited. Therefore, the if condition in this situation checks if the row is equal to 1 and the column is equal to 1 (range corresponding to the A1 cell) and based on that, it returns the current date which corresponds in this situation to the edit that has been made.
To track all the edits for a cell, you can store the date variables resulted from the edit to an array and eventually use it afterwards to your liking.
Note
Taking these into account, you won't be able to get the time of a cell which has been edited before having the trigger into place.
Reference
Drive API v3 Files:get;
Drive API v3 Files Resource;
Apps Script Event Objects;
Apps Script Simple Triggers.

In google sheets - how do I apply an onEdit trigger to run on just one cell on one sheet?

Thanks in advance for your help ...
I need to run a script upon editing just a single cell in a specific sheet.
A new row is first appended to the bottom of the dataRange.
The user first selects a date from a datePicker in Col A and then selects an entry from a list (using Data Validation) in Col B.
The idea is to use the textFinder to find the first previous row that matches the selected list item in Col B so that other cells on the new row can be populated from the found matched row.
All is working well except for the trigger.
Any ideas on how to trigger the script would be gratefully received!
Answer:
You can use an onEdit(e) trigger containing a conditional which only executes if a certain cell has been edited.
Code:
You can run whatever script you like on the edit of a specific cell by first checking the range of the edit using the event object:
function onEdit(e) {
if (e.range == 'A1') {
// put the code you want to run here
}
// you can put additional else if statements here if you want other code to
// execute on other cells being edited
else {
return;
}
}
Make sure to run the script manually one time first so that you can authenticate!
References:
Simple Triggers
Event Objects

Google Sheets: Cache IMPORTXML data

So I am trying to just create a simple Movie list for myself and want to keep the IMDB/Meta/RT scores in a cell.
I have it working fine with something like this:
=IF(ISBLANK(A2),,IFERROR(importxml(("http://www.omdbapi.com/?apikey=**MYKEY**&t="&A2&"&r=xml&tomatoes=true&y=2018"),"root/movie/#imdbRating")/10))
This will return a value that I can keep in the cell. The PROBLEM is that it doesn't always refresh. Either google sheets is bugged, importxml, or the omdbapi. I have read others having the same issue.
A typical row looks like this:
[Black Panther] [February 16, 2018] [86%] [74%] [97%] [88%]
Since my data does not change much I wanted to somehow cache it. So if the importxml fails it won't blank out the cell, it will just keep whatever the last value was. Maybe I can do this by referring to another tab's cell? I did try that, but as soon as the other tab cell gets blanked out(because the importxml fails) so does the main tab's cell.
Thoughts?
I believe this requires an apps script that will add each change you make to row 1, to a corresponding target column 2 - if not empty/NA:
function onEdit(e) {
var sh = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
if(sh.getActiveCell().getColumn() == 1) {
if(e.value && e.value != '#N/A')
sh.getRange(e.range.rowStart, 2).setValue(e.value);
}
}
Goto Tools, Script Editor, from the menu and add the script. Make sure to run it once in the debugger and grant the required permissions.
Also, take note of this references:
Refresh data retrieved by a custom function in Google Sheet
How to debug Google Apps Script (aka where does Logger.log log to?)
https://webapps.stackexchange.com/questions/42204/how-do-i-automatically-update-a-cell-in-google-spreadsheets-to-the-most-recently