Google Script Move selection to specific cell - google-apps-script

I'm trying to move the cursor (select cell) when the user clicks on the Sheet.
I've tried so many options and none of them work. The cursor just stays where I click, instead of moving to the specified cell.
Note: I change the background color of the clicked cell just to make sure the selection trigger is working.
function onSelectionChange(e) {
var range = e.range;
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
range.setBackground("blue");
sheet.setCurrentCell("a1").activate();
}
Any help greatly appreciated.

The setCurrentCell method is never executed because you are using the wrong parameter for it.
According to the documentation here, setCurrentCell expects an object of type 'Range`, however, you are passing a string to it.
In order to fix this, you should update your function to this:
function onSelectionChange(e) {
var range = e.range;
var sheet = range.getSheet();
range.setBackground("blue");
var cell = sheet.getRange("A1");
sheet.setCurrentCell(cell);
}
Also, since you are using the onSelectionChange trigger, for best practices, it is recommended you make use of the e event object, hence the modifications above.
Reference
Apps Script Spreadsheet Class - setCurrentCell(cell);
Apps Script Event Objects.

Related

Google apps script, onChange, can't pass a trigger object to a variable [duplicate]

This question already has answers here:
Google Spreadsheet Script onChange event not firing
(3 answers)
Closed 2 years ago.
This post was closed, but I have not been able to find a comparable problem elsewhere.
I'm trying to work out a script that will hide rows from an onChange() trigger. When a cell becomes "0", I want the row that cell is in to become hidden. Comments from my first post have taught me that the object passed from onChange does not contain a range. Is there a workaround that would solve this problem?
My spreadsheet has an input sheet for the backend and an output sheet for the frontend that goes to the client. I need an onChange trigger, so that as data goes to the frontend it nicely format for emailing to the client. Most importantly, I need empty ('0) rows to be hidden.
I'm new and just learning, so what I've written isn't working because onChange objects do not include a range. Thank you.
function onChange(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Client");
var cell = e.range;
var VALUE = cell.getValue();
if(VALUE == 0){
sheet.hideRow(cell);
}
}
I've also tried:
function onChange(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Client");
var cell = e.getValue();
if(cell == 0){
sheet.hideRow(cell);
}
}
There is no range value returned by an onChange trigger. Here's what the event object looks like.
{"authMode":"FULL","changeType":"INSERT_ROW","source":{},"triggerUid":"","user": {"email":","nickname":""}}
Addtionally keep in mind onChange requires an installable trigger.
onChange Event Object
It seems like you have the onChange onEdit triggers confused. The onChange trigger fires when a "larger" or structural change occurs, rather than the on changed values. From the docs:
An installable change trigger runs when a user modifies the structure of a spreadsheet itself—for example, by adding a new sheet or removing a column.
You can see from the event object specifications that there is no value passed to the programmer from the onChange trigger, and therefore the type of functionality you're looking for is not easily done.
You want to use the onEdit trigger which will function closer to what you are looking for. It will fire on any value change and give you the new and old values. onEdit is also a "simple trigger" so there is no need to install additional triggers like you do for onChange.
Using onEdit your code will look something like this:
function onEdit(e) {
val range = e.range;
val sheet = SpreadsheetApp.getActiveSheet();
if (e.value == 0) {
var rowNum = e.range.getRow();
sheet.hideRow(rowNum);
}
}

How to get an IFTTT action in Google Sheets to trigger a script?

I have an IFTTT applet that will update cell C65 in my google sheet. I'd like that sheet to run a function submitData() whenever cell C65 is changed. As many know, however, the onEdit and onChange functions do not trigger from IFTTT actions, only from manual inputs.
I've learned this from searching for answers, and nobody seems to be able to explain a workaround. The strangest thing here is that when I first put it together today, it actually worked perfectly. I then swapped google accounts and made other changes that didn't relate to the code or IFTTT applet and all of a sudden it stopped working entirely, even when switching back to the correct google account.
This is the onEdit code that works for screen inputs but not when my IFTTT enters a value into the cell:
function onEdit(e){
var cellAddress,cellAddressToTestFor;
cellAddressToTestFor = 'C65';
// Get cell edited - If it's C65 then do something
cellAddress = e.range.getA1Notation();
Logger.log('cellAddress: ' + cellAddress);
if (cellAddress === cellAddressToTestFor) {
//To Do - Code here if cell edited is correct
submitData();
};
}
Is there any way to get the IFTTT event to trigger my submitData function?
So the 'on edit' trigger won't work with IFTTT changes, but the 'on change' installable trigger will. However, the event object passed by the On Change trigger does not contain 'range', so there's no way to easily check the edited cell's location. To do so:
function onChange(e){
var cellAddress,cellAddressToTestFor;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var activeSheet = ss.getActiveSheet();
var activeSheetName = activeSheet.getSheetName();
cellAddressToTestFor = 'C65';
// Get cell edited - If it's C65 then do something
cellAddress = activeSheet.getActiveRange().getA1Notation();
Logger.log('cellAddress: ' + cellAddress);
if (cellAddress === cellAddressToTestFor) {
//To Do - Code here if cell edited is correct
submitData();
};
}

Trouble triggering a macro when a specific cell has a specific value

I'm trying to trigger a google sheet macro / apps script to reformat a sheet when a specific value is entered into a specific cell. I have been working on it to get it to work but without success.
Here is my code;
function onEdit(e) {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = SpreadsheetApp.getActiveSheet();
var cell = sheet.getRange('C2');
var cellContent = cell.getValue();
if(cellContent === 'Past campaigns - actual cashflows to date only') {
spreadsheet.getRange('7:12').activate();
spreadsheet.getActiveSheet().hideRows(spreadsheet.getActiveRange().getRow(), spreadsheet.getActiveRange().getNumRows());
spreadsheet.getRange('13:13').activate();
}
};
I'm new to macros and apps scripts. I have tried to implement all the suggestions from the following links without success;
How can I get a macro to automatically trigger when a cell reaches a certain value in a Google Sheet?
https://developers.google.com/apps-script/guides/triggers
https://developers.google.com/apps-script/guides/sheets
As #Cooper has said you cannot make a trigger based on a cell value. Only on user changes.
You could try to make the onEdit trigger to execute only inside an if statement. But that's already what you have done.
Try this:
function onEdit(e) {
e.source.toast('Entry');
const sh=e.range.getSheet();
if(sh.getName()!='Sheet Name')return;//you need to change this sheetname
const X=sh.getRange('C2').getValue();
if(X=='Past campaigns - actual cashflows to date only') {
e.source.toast('flag1');
sh.hideRows(e.range.rowStart,e.range.rowEnd-e.range.rowStart+1);
}
}
It's not necessary to use activate in most scripts and certainly in this one since it has to finish in 30 seconds. They use activate in macros because they are following you actions as you move around the screen. But in a script, generally to don't want to interact with the screen very much because it slows down the script and it doesn't really accomplish anything.
I'm currently using in a script that run when a Spreadsheet opens up because it has a lot rows in it and I want it to go down to the last row. So activate is useful because I don't have to scroll down to the last row every time I open the spreadsheet.
try it this way:
function onEdit(e) {
//e.source.toast('Entry');
var sh=e.range.getSheet();
if(sh.getName()!='Sheet Name')return;//you need to change this sheetnamee
var X=sh.getRange('C2').getValue();
if(X=='Past campaigns - actual cashflows to date only') {
//e.source.toast('flag1');
sh.hideRows(e.range.rowStart,e.range.rowEnd-e.range.rowStart+1);
}
}
I hope you're not trying to run this from the script editor because that's not going to work.

How to make each function run on specific page?

I have two fuctions with same name "myFunction" I run it using google trigers on edit, and I have two sheet how to specify each one for each sheet I use this but it doesn't work
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Copy of Timesheet & Feedback");
//and on the other function
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Visits");
Problem:
getSheetByName() will return the sheet you're looking for, but has nothing to do with the sheet that is being edited to trigger your function.
Solution:
Instead you can use event objects for this purpose. Wrap your whole script inside an if statement checking the name of the sheet. The new function should look something like this:
function myFunction(event) {
var ss = event.range.getSheet();
if (ss.getName() === "Copy of Timesheet & Feedback") {
//your code here
}
}
This way the code will not continue if it doesn't find the sheet it's expecting (the sheet name inside the if statement).
Notes:
Notice the name of the function myFunction(event), "(event)" is important so that we can access the event object to grab the spreadsheet that is being edited.
You won't be able to run the script manually, set up a trigger for "on Edit" and it'll just run automatically.
References:
Event Objects

How can I get a macro to automatically trigger when a cell reaches a certain value in a Google Sheet?

What I need is for a macro I've recorded called SwitchHotSeat to trigger when the value of cell F3 goes above £1,000,000.00 0r 1000000 or the LEN of cell F3 goes over that length.
I can find guides for doing this in Excel, but not for Google Sheets. below is just the code for my macro.
function SwitchHotSeat() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('3:3').activate();
spreadsheet.getActiveSheet().deleteRows(spreadsheet.getActiveRange().getRow(), spreadsheet.getActiveRange().getNumRows());
spreadsheet.getActiveRangeList().setBackground('#ff0000')
.setFontColor('#ffffff');
};
The macro works fine. I just need a way of triggering it when that cell goes over the 1000000.
You could use simple or installable triggers. See
https://developers.google.com/apps-script/guides/triggers
https://developers.google.com/apps-script/guides/triggers/installable
Example of using onEdit simple trigger
function onEdit(e){
if(/* add here your conditions */) {
SwitchHotSeat();
}
}
NOTE: on edit and on change triggers are triggered only when an user edit the spreadsheet.
Just in case it helps anyone else with a similar issue, I discovered (by logging) that it was returning an object, rather than a number before, so the following code worked fine in the end.
function onFormSubmit(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var millionaire24 = ss.getSheetByName('Millionaire24.');
var cellValue = millionaire24.getRange(3,6,1,1).getValues();
Number(cellValue)
if(cellValue[0] >= 1000000)switchHotSeat();
}