onEdit function code not changing when I redeploy my addon - google-apps-script

I am writing a Google Sheets Addon, and when I try make changes to my onEdit(e) function, they simply do not reflect. I can make changes to other parts of my addon and it all seems to go through, but right now my onEdit(e) function turns the edited cells red, and nothing - not even deleting the onEdit(e) function - will change that behavior.
I have tried the following:
Changing the code in my script to change the cell to be yellow instead. Then I Test my script as an addon, choose a brand new sheet that I've just created - and yet the onEdit(e) acts as before.
I've tried checking my current project's triggers, but the list is blank, as well as all my simple triggers in general.
I've tried deleting the onEdit(e) method entirely from my project, and deploying that, but it's still there, working on edits.
Not that I think the actual code matters, but my onEdit looks like this:
function onEdit(e) {
const range = e.range;
range.setBackground('#ffe599');
}
Nothing I seem to try will change or remove that behavior from my addon now :/
Please note I am refreshing the page, and even creating entirely new sheets to install the addon from scratch for each attempt to change this thing.
Any help would be awesome as I'm losing my mind a bit! Thanks in advance! :)

I wrote it this way:
function onEdit(e) {
if(e.range.getSheet().getName()=='Sheet1') {
e.range.setBackground('#ffe599');
}
}
And it works for me. It changes the background when the range is edited by a user.
When I delete the function the background doesn't change on edits any longer.
If I rename the function to something other than onEdit is stops working as well.
I made a little change:
function onEdit(e) {
if(e.range.getSheet().getName()=='Sheet1') {
e.range.setBackground('#ffe599');
e.range.offset(0,1).setBackground('#ffff00');
}
}
And the changes the background of the cell edited and the one next to it to the right.

Related

How to setup the "onChange" trigger in Google Sheet script editor

I need to get a Google sheet's cell background color hex code into that same cell.
This is the function I currently use:
function BGHEX(row, column) {
var background = SpreadsheetApp.getActive().getDataRange().getCell(row, column).getBackground();
return background;
}
Now I have 2 issues:
I need the function to run again when the cell's background color is edited. I know I need to use the installable trigger "onChange" but no idea how to set it up.
I need this script & function to run on a duplicate of this Google Sheet file (meaning other sheet id's).
Thanks in advance for your help!
Follow these steps to setup an onChange trigger in your spreadsheet
Open your Apps Script project.
At the left, click Triggers alarm.
At the bottom right, click Add Trigger.
Select and configure the type of trigger you want to create.
Click Save.
Your trigger setup should look like this:
To detect background change in your spreadsheet, you need to have an Event Object to determine which changeType the user made. For this scenario, we need 'FORMAT'.
Also, to change the background color of a cell or range, the user must click or highlight the range. Using getActiveRange() will help us determine which range is being edited.
Code:
function onBGChange(e) {
if(e.changeType == 'FORMAT')
Logger.log(SpreadsheetApp.getActive().getActiveRange().getBackground());
}
Example:
Here I change the background color of A1
Output:
Executions Tab:
To apply this to another spreadsheet, you need to replicate the code and trigger set up. A script is bound to the spreadsheet where it was created and functions like getActive() and getActiveRange() will only work on the spreadsheet the script was bound to.
References:
Installable Triggers
Event Object
getActiveRange()

Copy the text of a cell after an hyperlink click to another cell

I have a list and all the text is hyperlinked to the same cell in the same sheet.
Is there any way to copy the text of the cell you've clicked to the other cell?
A B
=HYPERLINK("...range=B1";"apple")
=HYPERLINK("...range=B1";"banana")
=HYPERLINK("...range=B1";"pear")
=HYPERLINK("...range=B1";"melon")
Explanation:
Your goal is to click/select a cell and get the value copied to the next cell in the same row.
You can use a onSelectionChange trigger to achieve your goal.
Solution:
Copy this code to the script editor:
function onSelectionChange(e) {
if(e.range.getColumn()==1){
e.range.offset(0,1).setValue(e.range.getValue());
}
}
Illustration:
Updated code based on OPs comment:
Copy all hyperlinks to cell B1:
function onSelectionChange(e) {
if(e.range.getColumn()==1){
e.source.getActiveSheet().getRange('B1').setValue(e.range.getValue());
}
}
Please Note!
Users have reported in the IssueTracker that onSelectionChange trigger does not work for them and it didn't work for me either. However, after I tried the recommendation steps by the google guy (you can find them in the link also) it worked:
However, I managed to get the trigger to work correctly by
deactivating V8 runtime, closing the Apps Script project and the
document, and finally, reopening the document and reactivating the V8
runtime.

google application script assign function when cell is clicked

I am attempting to make many buttons on a Google Sheets spreadsheet that will record my cryptocurrency trades in a format that is specific to my trade strategy. The details of that strategy are not important for this issue. I only want to run a relative script whenever the (Trade!) button is clicked. I want to copy a lot of them, so it wouldn't make sense to make an individual macro for each buy/sell range. Two possible directions for a solution are clear to me, but I can't find information on either.
Possible solution 1: Assign a macro to a cell when it is selected and put the button inside the cell. I can do this with VBA, but cannot find instructions on how to do this for Google Sheets Script.
Possible solution 2: Have the function select the relative cell beneath the button that was clicked. Targeting cells relative to other cells is easy enough, but I can't seem to find a command for targeting a cell relative to an image. Again, this is doable in Excel with VBA, but I can't find the same function for Google Sheets Script.
Any help is appreciated.
You can use checkboxes and this onEdit() code to call whatever function you wish.
function onEdit(e) {
//e.source.toast('Entry');
//Logger.log(JSON.stringify(e));
var sh=e.range.getSheet();
if(sh.getName()=='Trades' && e.range.columnStart==7 && e.range.rowStart>2 && e.value=="TRUE") {
e.range.setValue('FALSE');
e.source.toast('You clicked cell ' + e.range.getA1Notation());
//call whatever function you wish to call
}
}
Animation:

Google Apps - Script - Side bar OnOpen/OnInstall function

I'm recently having issues with Google Apps Scripting, I have a script which installed domain wide, which is enables users to make some tasks easier with the side-bar menu however from last week side-bar menu is not popping up anymore. Nothing has been changed on the code.
I did some workaround and tried all possible ways - deauthorization \ authorization, deploying the same code with different name, nothing worked except if the user removes it and adds it to the spreadsheet which they work - it's popping up and working only for that sheet and only for one time.
Can you please advise, how to fix this issue?
Here is my OnInstall and OnOpen functions:
function onInstall(e)
{
onOpen(e)
}
function onOpen(e) {
var html = HtmlService.createHtmlOutputFromFile('Index')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setTitle('BB KING');
SpreadsheetApp.getUi().showSidebar(html);
}
Thanks in advance.
Edit : Triggers making the side-bar work on simple scripts on spesific spreadsheets, but this method is not working on domain-wide installed add-ons.
It should be possible to add a trigger to run the custom function which shows the SideBar.
I have a function like the following in my code that runs on my domain:
function createOnOpenTrigger()
{
var sheet = SpreadsheetApp.getActiveSpreadsheet();
// Trigger on open
ScriptApp.newTrigger(FunctionToRunOnOpen)
.forSpreadsheet(sheet).onOpen().create();
}
function FunctionToRunOnOpen()
{
var html = HtmlService.createHtmlOutputFromFile('Index')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setTitle('BB KING');
SpreadsheetApp.getUi().showSidebar(html);
}
You could then put the createOnOpenTrigger function as an option in your custom menu - to be run only once everytime a copy is made of the entire spreadsheet.

onEdit function

in google apps script, how can I automatically run a script when I change the background color of a cell?
When I change the background color of a cell, the spreasheet is automatically saved and it says also when last edit was made, but if I try to use this event on an onEdit function to run a script, it doesn't recognize the event and does nothing. Thanks
onEdit is going to fire when any cell value changes, not with any formatting changes. You'll have to place the rules to change the bg colors in the script, assuming that the rules are based on cell values already in the sheet.
I'm afraid you'll have to manage this using a 'trick' as there is indeed no automatic trigger for background color changes.
Just a suggestion : If your spreadsheet is not too big you could simply memorize the whole color description in a string you previously stored in script properties and, based on a timer trigger, check if something has changed.
If this solution is acceptable for you (the timer trigger and not an immediate reaction) give it a try...
Use this var for example :
var backGroundString = SpreadsheetApp.getActiveSheet().getDataRange().getBackgrounds().toString()
and compare it to the same value stored in scriptProperties, if different, trigger your event...
I know it's a bit clunky but right now I see no other simple way to get what you want.
You could add a few conditions to make it work only at some moments or when the spreadsheet is open...