Reputation: 145
I'm developing an app for an organization and they use Google Sheets a lot, about half of them uses the mobile Sheets app.
Now, I want to have an UI Alert if they change some cells' values. This works fine on the desktop browser (Chrome PC, Firefox etc..) but it doesn't work on the mobile app. I know it wouldn't work, and it's messing up my code because after the alert there are some other codes to execute, example:
function showAlert(message) {
var ui = SpreadsheetApp.getUi();
var result = ui.alert(
'Please confirm',
message,
ui.ButtonSet.YES_NO);
// Process the user's response.
if (result == ui.Button.YES) {
// User clicked "Yes".
return true;
} else {
// User clicked "No" or X in the title bar.
return false;
}
}
function Test(){
if(showAlert("Are you sure?")){
Logger.log("User says yes");
}else{
Logger.log("No");
}
}
The execution time for a function in Google App Script is 300 seconds, and while the alert didn't show, the timer continued waiting for the user input. The result is the script timed out without executing other codes, like this:
If there's a way to detect if an user is on mobile I can skip the alert so the codes can be executed, something like this
function Test(){
if( user not on mobile && showAlert("Are you sure?"){
Logger.log("User says Yes");
}
}
Is this possible? Or can I wait for 1 minute, if there's no response from the user, skip the alert and continue with other code?
Upvotes: 4
Views: 1856
Reputation: 2770
Short answer, it's not possible. Long answer, it is, if you are using a webapp instead of the SpreadsheetsApp.getUI().
That would mean you'd have to rewrite your whole application, so keep that in mind.
If you are using a webapp you could use this reference and modify it to suit your needs like this:
UserBrowserAgent.hmtl
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<div id="response">
</div>
<script>
if (/Mobi/.test(navigator.userAgent)) {
// mobile!
document.getElementById("response").textContent = "mobile";
} else {
// something else
document.getElementById("response").textContent = "other";
}
</script>
</body>
</html>
Web app that calls the page
function doGet() {
return HtmlService.createTemplateFromFile("UserBrowserAgent").evaluate().setTitle("User Agent - Google Apps Script");
}
Hope this helps!
Upvotes: 4