如何使用Google App Script检测移动用户?

4

我正在为一个组织开发一款应用程序,他们经常使用Google Sheets,大约一半的人使用移动版Sheets应用程序。

现在,如果他们更改某些单元格的值,我想要有一个UI警报。这在桌面浏览器上(Chrome PC、Firefox等)运行良好,但在移动应用上不起作用。我知道它不会起作用,并且它正在破坏我的代码,因为警报后还有一些其他代码需要执行,例如:

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");
   }
}

在Google App Script中,函数的执行时间为300秒。在弹出警报未显示的情况下,计时器会继续等待用户输入。结果是脚本超时,未执行其他代码,就像这样:

Example of a failed execution due to waiting for user input!

如果可以检测到用户正在使用移动设备,我就可以跳过警报,让代码得以执行,类似于这样:
function Test(){
    if( user not on mobile && showAlert("Are you sure?"){
         Logger.log("User says Yes");
    }
}

这是否可行?或者我可以等待1分钟,如果用户没有回应,则跳过弹出窗口并继续进行其他代码执行吗?

1个回答

4
简短的回答是不可能的。长话短说,如果您使用的是Web应用程序而不是SpreadsheetsApp.getUI(),那么就有可能了。这意味着您将不得不重写整个应用程序,请记住这一点。如果您使用Web应用程序,可以使用此参考并像这样修改以适应您的需求

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 应用程序

function doGet() {
  return HtmlService.createTemplateFromFile("UserBrowserAgent").evaluate().setTitle("User Agent - Google Apps Script");
}

希望这有所帮助!

这不是一个Web应用程序,感谢您提供的信息性答案。如果我想让它工作,我想我会选择模态对话框路线。 - catmandx

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接