Can I use Slides API to add an “Agree to Terms” dialogue before allowing viewer access to a Slides presentation?

浪子不回头ぞ 提交于 2021-01-29 10:16:23

问题


I have a Google Slides presentation that I'd like to gate with a request to agree to "Terms & Conditions" before viewing. (The dialogue box would include an external link to the legal fine print.)

I see that with Google App Script adding a dialogue box is possible, but if one can be customized in this manner is unclear to me.

Thanks in advance for any help on this.


回答1:


  • You want to open a dialog, when users open the Google Slides.
    • In this case, users are logged in to each Google account.
  • You want to make users show "Terms & Conditions" by the external link.
  • When users click "ok" button, when users reopen the Google Slides, you don't want to open the dialog.
  • When users click "cancel" button, when users reopen the Google Slides, you want to open the dialog.
  • You want to use a custom dialog for this.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

Issue and workarounds:

In order to open the custom dialog when users open the Google Slides, it is required to use the installable trigger. But at Google Slides, unfortunately, in the current stage, the OnOpen event trigger cannot be used as the installable trigger. By this, when users open the Google Slides, the custom dialog cannot be opened. It seems that this is the current specification. So in order to achieve abour your goal, it is required to think of the workaround. In this answer, I would like to propose the following 2 workarounds.

Workaround 1:

Fortunately, at Google Slides, the simple trigger can be used instead of the installable trigger which cannot be used. In this case, the OnOpen event trigger can be used as the simple trigger. So in this workaround, the built-in dialog is used with the simple trigger.

Sample script:

Please copy and paste the following script to the container-bound script of the Google Slides and save it. When you open the Google Slides, a dialog is opened. When "ok" is clicked, keyObject is set to the PropertiesService. By this, when you open the Google Slides again, the dialog is not opened. In this case, getUserProperties() is used. So keyObject can be used for each users.

function onOpen() {
  var keyObject = {key1: "value1"};  // Here, key and value for checking whether user had clicked "ok" button.

  var prop = PropertiesService.getUserProperties();
  var value = prop.getProperty(Object.keys(keyObject)[0]);
  if (value != keyObject.key1) {
    var ui = SlidesApp.getUi();
    var result = ui.alert('sample title', 'Please check this link.\nhttps://###/', ui.ButtonSet.YES_NO);
    if (result == ui.Button.YES) {
      ui.alert('"ok" was clicked.');
      prop.setProperties(keyObject);
    } else {
      ui.alert('"cancel" was clicked.');
    }
  }
}
  • In this case, the URL cannot be clicked. So user is required to access to the URL. I think that this is the limitation when this workaround is used.
  • If you want to delete keyObject. Please use the following script.

    function deleteProperty() {
      var keyObject = {key1: "value1"};
      var prop = PropertiesService.getUserProperties();
      prop.deleteProperty(Object.keys(keyObject)[0]);
    }
    

Workaround 2:

In this workaround, the built-in dialog and the custom menu are used with the simple trigger. In this case, the custom dialog is used.

Sample script:

Please copy and paste the following script to the container-bound script of the Google Slides and save it. When you open the Google Slides, a dialog is opened. The dialog shows "Please open dialog from the menu.". Users click "ok" button and open the custom dialog from the custom menu. When "ok" is clicked at the custom dialog, keyObject is set to the PropertiesService. By this, when you open the Google Slides again, the dialog is not opened. In this case, getUserProperties() is used. So keyObject can be used for each users.

function onOpen() {
  var keyObject = {key1: "value1"};
  var prop = PropertiesService.getUserProperties();
  var value = prop.getProperty(Object.keys(keyObject)[0]);
  if (value != keyObject.key1) {
    var ui = SlidesApp.getUi();
    ui.createMenu('Open dialog').addItem('Open dialog', 'openDialog').addToUi();
    var result = ui.alert('sample title', 'Please open dialog from the menu.', ui.ButtonSet.OK);
  }
}

function openDialog() {
  var ui = SlidesApp.getUi();
  var html = '<a href="https://###/" target="_blank">link</a><input type="button" value="ok" onClick="ok()"><input type="button" value="cancel" onClick="cancel()"><script>function ok() {google.script.run.withSuccessHandler(()=>google.script.host.close()).setProp()}function cancel() {google.script.host.close()}</script>';
  ui.showModalDialog(HtmlService.createHtmlOutput(html), "sample");
}

function setProp() {
  var keyObject = {key1: "value1"};
  var prop = PropertiesService.getUserProperties();
  prop.setProperties(keyObject);
}
  • In this case, the URL can be clicked. But in order to open the custom dialog, it is required to open it from the custom menu. I think that this is the limitation when this workaround is used.

Note:

  • Above scripts are the simple scripts for testing the workaround. So please modify them for your actual situation.

References:

  • Simple Triggers
  • Installable Triggers
  • Alert dialogs
  • Custom dialogs

If I misunderstood your question and this was not the direction you want, I apologize.



来源:https://stackoverflow.com/questions/59990535/can-i-use-slides-api-to-add-an-agree-to-terms-dialogue-before-allowing-viewer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!