Simple `onEdit` Function Not Running for Google Sheets - google-apps-script

I have a simple onEdit function that I use on other sheets. I have had no issue when using this in the past. As of late, this function does not run.
function onEdit(e) {
var sheet = e.range.getSheet();
var name = sheet.getSheetName()
Logger.log(name)
if(e.range.getA1Notation() === "C3" && name == 'Main') {
sheet.getRange("C4").clearContent()
}
}
I even added the Logger.log() call to check and make sure I wasn't insane. But nope, it won't run.
I have even tried making a test sheet with no additional code to interfere with the onEdit call... no dice.
Any ideas? I'm pretty lost on this one.
Thanks,
Marcus

Based on the script you posted, the function would only be applicable to a sheet named "Main" and would only affect Cell C4 if Cell C3 is edited.
First of all, Logger.log would not show anything if the script was not initiated from the script code itself, therefore, it would not be possible to check that way. Therefore, I checked using var ui = SpreadsheetApp.getUi() and creating an alert. everytime i made an edit.
Secondly, based on that check, the script that you provided did indeed run. Therefore, this leaves 4 possible sources of error. 1) the sheet that you are making the edit on is not named "Main". 2)the cell you are editing is not cell C3, 3) the cell C4 is empty and there is nothing to clear 4) You did not make the edit, i.e. the change in C3 was not yet input into the cell because you have not keyed in enter or selected a place outside of the cell.

Related

Google Sheets, how to run function every x column?

I have a script in google sheets with the following function:
// Function to get current active sheet-name.
function getsheetName(dummy) {
return SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName();
}
Currently this function runs with this code in sheets:
=getsheetname(3:55)
The reference targets my entire sheet.
So whenever I make a change to a cell in my sheet the function runs again. This causes a huge amount of loading everytime i type anything in a new cell. Can i somehow reference the function to only run when i make changes to every x column instead of every cell?
I tried changing the formula but it would just stop updating completely, tried combining strings and using the MOD operator but I couldn't figure it out (keep in mind i am very new at this so probably missing something in my tinkering).
Appreciate any help!
I believe your goal is as follows.
You want to refresh the custom function of =getsheetname() when the columns "J", "T",,, (every 10 columns).
In this case, how about the following sample script? In order to refresh the function on Google Spreadsheet, I used this method. Ref
Sample script:
In this sample, in order to refresh your custom function, the simple trigger of onEdit is used. Please copy and paste the following script to the script editor of Spreadsheet. And, please save the script. When you use this script, please edit the columns "J", "T" and so on. By this, the script is run.
function onEdit(e) {
if (e.range.columnStart % 10 != 0) return;
const functionName = "=getsheetName";
const temp = "=temp";
const sheet = e.source.getActiveSheet();
sheet.createTextFinder(functionName).matchFormulaText(true).replaceAllWith(temp);
sheet.createTextFinder(temp).matchFormulaText(true).replaceAllWith(functionName);
}
In this case, 3:55 of =getsheetname(3:55) is not required to be used. You can use just =getsheetname().
If you changed the function name of your custom function, please modify const functionName = "=getsheetName";.
Reference:
Simple Triggers

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.

Using Apps Script to return to certain cell and make it editable-ready in Google Sheet

I'm quite new to Apps Script. I hope someone could help me please.
I wanted to create an Apps Script in Google Sheet where the script will return to cell C4 and make it editable-ready. My script is as below:-
function EditC4() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('C4').activate();
}
Problem with this script is that just made C4 active but it is un-editable. Pressing F2 when the C4 is being made 'active' does not work as well. I'm calling the EditC4 function from another function.
I prefer not to use "Browser.msgBox" to make it edit-ready. Could someone please help? I search everywhere but could not find the answer.
Explanation:
The activate() method is used mainly by macros to register some cells or ranges as active and then use getActiveRange() or getActiveCell() to get these ranges back.
In your case, you want to "highlight" or "mark" a particular cell so it can be edited directly with the keyboard.
In this case you want to use activateAsCurrentCell() .
Also it is a better practice to get the range object from the sheet object and not directly from the spreadsheet object.
Solution:
function EditC4() {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getActiveSheet();
sheet.getRange('C4').activateAsCurrentCell();
}
Illustration:

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

Copy Worksheet to New Workbook Doesn't Allow onEdit Apps Script to Run Properly

I created a Google spreadsheet template with a worksheet that's called "CollegeTracker" that users can right click on the tab, and "copy to" their workbook. However, my onEdit function from the Apps Script is not working.
function onEdit()
{
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "CollegeTracker" )
{ //checks that we're on the correct sheet
var r = s.getActiveCell();
var range = s.getRange(r.getRow(),r.getColumn());
var data = range.getValue();
var schoolRange = s.getRange(r.getRow(),1);
var schoolData = schoolRange.getValue();
var deadlineRange = s.getRange(r.getRow(),3);
var deadlineData = deadlineRange.getValue();
Logger.log(r.getRow());
Logger.log(r.getColumn());
Logger.log(data);
This is the beginning of my code. I added a couple loggers to see what's going on. I typed something in cell A2 but the logger shows row 1 column 1, or A1. I tried in a couple other cells but it always return A1 as the active cell. I repeating this in cell A6 and E7 but the logger always return A1. However, this is reading correctly in the template. I wonder if it's a permissions issue where I have to allow getActiveCell() to work. If this is the case, how would I go about doing this? I copy and pasted the code from my template and pasted into the script editor and it works fine. I can certainly make multiple copies of my script but if I figure this out, I think one copy can work for multiple sheets should be fine. Do you know what's blocking getActiveCell() from working? And if it's the permissions, how do I go about solving this?
Update:
I've been continuing my tests and realized that getActiveSheet() is still thinking that my template sheet is active even though I am working on a new sheet. I wonder if there is a way to determine the sheet I'm working in regardless of the Workbook I'm in.
Thanks!
-Ray