When I open a hidden sheet by a link, the system shows me notification that it's hidden with Unhide
button:
When I click on Unhide
I want to execute a script. Is it possible?
When I open a hidden sheet by a link, the system shows me notification that it's hidden with Unhide
button:
When I click on Unhide
I want to execute a script. Is it possible?
In the current stage, there are no methods for directly running the script by clicking "Unhide" button in your situation. So, in this case, it is required to use a workaround. In this workaround, onSelectionChange
of the simple trigger is used. When onSelectionChange
is used, the change of tab can be detected. Ref When "Undide" button is clicked, the tab is changed. This workaround uses this situation.
Please copy and paste the following script to the script editor of Spreadsheet.
function onOpen(e) {
const prop = PropertiesService.getScriptProperties();
const sheetName = e.range.getSheet().getSheetName();
prop.setProperty("previousSheet", sheetName);
}
function onSelectionChange(e) {
const prop = PropertiesService.getScriptProperties();
const previousSheet = prop.getProperty("previousSheet");
const range = e.range;
const sheetName = range.getSheet().getSheetName();
if (sheetName != previousSheet && range.getRichTextValue().getLinkUrl() != "") {
// When the tab is changed, this script is run.
range.setBackground("red");
}
prop.setProperty("previousSheet", sheetName);
}
In order to use this script, please reopen Google Spreadsheet. By this, the tab name is stored to PropertiesServices. The detection of change of tab uses this information.
And, in this case, as a sample situation, it supposes that "Sheet2" is hidden. And, the cell link of "Sheet2" is put in "Sheet1". When this link is clicked, a dialog is opened. When "Unhied" button is clicked, the tab is changed. And this change of tab is detected by onSelectionChange
. By this, the script can be run.
The demonstration is as follows.
onSelectionChange
cannot use all methods of Google Apps Script. Ref Although I'm not sure about your script, I thought that this is an important point of this workaround.