Check which sheets are currently being viewed in your spreadsheet - google-apps-script

to explain the issue briefly, there are certain sheets in the spreadsheet that are supposed to be hidden constantly.Though they have to be opened sometimes to change some data on them. The issue is that there are multiple collaborators on the spreadsheet getting annoyed by people leaving these sheets open and not hiding them again after they're done.
I have a lot of stuff on the sheet automatized through a minutely cycle anyhow so my first approach was to just hide them every cycle if they've been left open. The problem there is that the hideSheet() figuratively slaps the sheet out of your hand even if you're actively viewing it which shouldn't be happening.
I know that there are functions like getActiveSheet() and after some testing I've seen that this function, if you run it for yourself, returns the sheet you're currently viewing (like I myself am viewing).
My idea to overcome the problem that the hideSheet() function also closes the sheet you're currently viewing was to check whether the sheet is currently active for someone on the spreadsheet.The "myself" case obviously works here as I can just check whether I am viewing the sheet as a case for hiding it or not.
Long story short, does anyone have an idea how I can get a list of sheets that are currently being viewed or like being open on other peoples' ends?
Or if that's not possible to answer, does anyone know how I can ensure that the sheets are being rehidden on a regular basis without having the problem that I might just throw someone out of the sheet while he still has it open?

Try the change installable trigger.
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.
It might be triggered by showing/hiding a sheet. If so, your script could log when a sheet was shown then do something with that like polling the active sheet when the edits / change the spreadsheet and if the active sheet is not the same, then hide the sheet that was shown.
Reference
https://developers.google.com/apps-script/guides/triggers/installable

Related

ActiveSheet().getName() returns different results. Could somebody explain why? [duplicate]

I guess this question is the most similar to my current question. But basically, I have a google sheets file that has several sheets along the bottom. I would like for the user to execute this script on whatever sheet they have open at the time.
And like the previous question said according to this documentation, the active sheet is the one that is being displayed in the spreadsheet UI. I assume this means if I have the google sheets file open in another tab, that the sheet currently selected and viewable by me is intended to be the active sheet.
However, the actual active sheet returned is always the leftmost sheet along the bottom tabs. I've confirmed it by switching the order of the sheets. So no matter what sheet I actually have open and visible in the UI, it always gets the leftmost sheet. Here is the line that gets it.
var Sheet = SpreadsheetApp.openById("redactedid").getActiveSheet()
Is this a known error with App Script? Or is the documentation wrong? If it is, is there a workaround for getting the currently open sheet in the UI? I don't want anyone to have to hardcode a sheetname in the code as the file is constantly changing and sheets are being added.
Notes:
To use getActiveSheet() or any active object, the calling
Script must be bound to the Spreadsheet
Script function should be invoked from menu or button or macro keyboard shortcuts and NOT from webapp or API( Script editor also works, but not preferred).
Spreadsheet must be open/visible in the user interface/browser preferably in the active/currently selected window.
All previous calls to get objects must have used active. For eg, To get active range, You should have first got active spreadsheet => active sheet => active range(the chain of active objects). If any of the calls is not to the active object(say if you used getSheetByName() instead of getActiveSheet() in the middle step), the final call is likely to default to the default object(First sheet A1).
Solution:
Here, To get Spreadsheet, use SpreadsheetApp.getActive() instead of SpreadsheetApp.openById(), So that getActiveSheet() will be under the chain of active objects.
Snippet:
const sheet = SpreadsheetApp.getActive().getActiveSheet();
It's known that the getActiveSheet method returns the first sheet when used with an spreadsheet that is not the active spreadsheet.
The active spreadsheet is retrieved by using getActiveSpreadsheet. I only works on scripts bounded to an spreadsheet and in G Suite Editors add-on for spreadsheets.
In the the same way, getCurrentCell and getActiveRange returns A1 when corresponding sheet is not an active sheet (meaning it's not a sheet from an active spreadsheet)
Resources
https://developers.google.com/apps-script/guides/sheets
Related
Google Apps Script, select a sheet - Sets values in first sheet tab - not a specific sheet tab
All the points in the accepted solution are all true, but sometimes your spreadsheet looses some connection to something on Google's back end or the cache gets corrupted, who knows. The fix is to close the all tabs for the worksheet, editor, triggers, executions -- whichever ones you have open for that spreadsheet, close them. Reopen the spreadsheet. Start again. It's fine now.

Recording any copies made of a google sheet

I would like to create a Google sheet that records any copies that have been made of another Google Sheet even when the user creates a copy by putting 'copy' into the URL (https://docs.google.com/spreadsheets/d/"spreadsheet-ID"/copy).
I have found a solution on the post: Track number of copies made from a google spreadsheet but this doesn't record copies made when changing the URL as the above example.
I would like to know the whole URL of the sheet preferably but just the Spreadhseet ID would still be enough for me.
Has anyone got any ideas how I can record all this data onto a spreadsheet please?
Basically you won't have access to see who is copying your sheet if you use the built-in Make Copy.
What you could do is integrate part of the question you have provided, while disabling the built-in sharing method. So the only way to copy your sheet could be through a custom function you can control and register every time it executes.

Navigation in published Google Sheets

I need to publish a report I have in Google Sheets, but since the report is very big, with many sheets and tables, I have hyperlinks set up for easy navigation, that take you to ranges in other sheets.
When I publish the sheet, the hyperlinks stop working (they take you to the first sheet in a new browser tab). I also tried with a script to change the pages with a button, but the button is not clickable in the published page.
Thanks for any tips you might have.
EDIT:
I've prepared a test sheet to see if I find a solution for this. This is a link with permissions to edit:
https://docs.google.com/spreadsheets/d/1ZGw_6WjrkcNKdFvS8gIG46gEMfMuw7ex86SR9C7qXTU/edit?usp=sharing
And this would be the published version:
https://docs.google.com/spreadsheets/d/e/2PACX-1vTa8JDNMzwdvk87kCvbjJXYgK2RGiKy503eJn6eEjxbyU8oIsuvuKTNXCM6yRP16KXrnD9yvLV3J488/pubhtml
This actually works in Excel, I can embed the report and the hyperlinks still work fine, but I have everything else in Google Sheets, so I'd like to find a workaround.
You need to use proper query parameters. The sheet id, gid must be set to navigate properly. You cannot use rangeid. You can however use range.
/pubhtml?chrome=false&gid=[YOUR_SHEET_ID]&range=A1:B1
You can get your sheet id by visiting your sheet(tab) in your spreadsheet (edit version) and inspecting the url.
Your published sheet probably retains the original #gid (which is Google's sheet ID used for local links). This will fail because it is linked to a Spreadsheet that is inaccessible to the new report. Please check if this is the case (you'll see in the links you've created). I can understand that you won't want to share the original reports but you can set up a small test Spreadsheet with a few linked sheet ranges to test the process - please share that.

Script Execution when Switching Sheets withing the Same Spreadsheet

I'm running an online booking sheet for my local squash club (dragging them into this century!) and need to find an easy way to automate navigation to the current date when #1 opening the spreadsheet, an #2 switching sheets while the spreadsheet is open. Without the feature, people will have to endlessly scroll through the season to find today's date.
I've run a formula in column A that will place an * on the row that =TODAY() and would appreciate a bit of advice from people with script writing skills as to whether it would be possible to go to that cell on start up, and when we switch between sheets (we run 2 courts, on separate sheets).
As always, help is very much appreciated, and hopefully there is something that can be introduced that can help.
[edit] I suppose the simplest way, looking back at it now, would be the following I just need to learn how to code it:
OnOpen - Set the active sheet as "COURT 1"
Set the active range for the function as A:A
Go to the last modified cell in that range (as it's always going to the be the cell that I want).
The script should then repeat the process for "COURT 2" and then return to "COURT 1".
Copy of the spreadsheet can be found here:
https://docs.google.com/spreadsheets/d/1NBom_qB9AM_LG2lgrGjNRDbxbSGysVj8H6AXqdgKWD8/edit?usp=sharing
Thanks :)
Mark
Some key concepts that might help. I highly advise that you learn how to script so you don't go asking for code each time you want to modify something.
To run a script upon opening the sheet, you can make use of onOpen() trigger. On the same note, you need to be aware of this trigger's Restrictions.
To operate on another sheet within the same SpreadSheet, use setActiveSheet(sheet). When the sheet becomes active, you can now do operations on it. Check the SpreadsheetApp for more docs info.

Bound app scripts and multiple projects

First time I'm working with google app scripts. I inherited a google sheet (template) from a colleague (who no longer works with us) that runs some scripts on change. The script from what I read should be a bound script.
When I open Script editor though, I see 2 projects. One is the copy of an earlier version of the project. Each of them has an onOpen() function. My question is how does the sheet know which one to invoke?
To replicate the behaviour, I created a sheet of my own and then using the script editor, created two projects each with the onOpen function. The function adds some menus on opening the sheet.
I see that both functions are fired, because the menu names are different. However, I don't see the same behaviour in the original template in question which seems to invoke only one of the two scripts.
I checked to see if there are any triggers etc. but none seem to exist. I find no documentation around this either.
They are both bounded to the sheet, as your tests showed both onOpen(e) are being invoked. What you could do to know which of the script was edited last or some details is to check the revision history of each script (in each script file-> see revision history).
I figure the last developer didn't comment the code so he must've disaled one of them by hand
It turns out as I mentioned that both onOpen functions were indeed being fired. Both scripts and tons of code in it but was exactly identical and the end effect was the same on the sheet. Pretty weird.
I fixed it by making a copy of the sheet which let me delete one of the projects and keep just one. I then deleted the original sheet and made my copy the official one (naming it the same).
I'd have preferred if apps script chose to make it unambiguous by mandating explicit triggers to a function on a particular sheet.