0

I'm trying to create a game in Google Slides and need to have a system where if the user is on a certain slide, a variable is changed. How is this possible?

I have already tried to use SlidesApp.getActivePresentation().getSlides() SlidesApp.getActivePresentation().getSelection().getCurrentPage();

var currentPresentationSlide = SlidesApp.getActivePresentation().getSlides()[3];
var currentPage = SlidesApp.getActivePresentation().getSelection().getCurrentPage();

var selection = SlidesApp.getActivePresentation().getSelection();


if (currentPage = currentPresentationSlide) {
  var shape = currentPresentationSlide.insertShape(SlidesApp.ShapeType.TEXT_BOX, 100, 200, 300, 60);
  var textRange = shape.getText();
  textRange.setText('demo');
}

I want Slides to place the text box (demo) on the slide (in this case it slide 3) It doesn't even place the text box anywhere.

  • I haven't messed with slides much but I know that there's an [advanced slides API](https://developers.google.com/slides/reference/rest/) perhaps that can help. – Cooper Jan 11 '19 at 01:16
  • I've looked into that –  Jan 11 '19 at 01:40
  • Google Slides may not be robust enough to support a game. What kind of game are you trying to implement? Maybe you can leverage Google Sheets to pull it off since its the most mature in terms of capability (of all the GSuite Apps). – TheAddonDepot Jan 11 '19 at 13:35
  • You have an error in your conditional check. Please consider replacing `if (currentPage = currentPresentationSlide) {`, by something like `if (currentPage.getObectId() === currentPresentationSlide.getObjectId()) {`. Notice that I've used `===` (not `=`) and that the conditional logic is based on strings, not objects. JavaScript generally returns unexpected results when comparing objects. – Frenchcooc Jul 26 '21 at 09:11

3 Answers3

0
  • User is presenting by the presentation mode.
  • Under above situation, you want to retrieve the current page that the user is staying.

I understand about your question like this. Unfortunately, in the current stage, this cannot be achieved by the following reasons.

  1. There are no methods for confirming whether user who is not owner is staying in the presentation mode, yet.
  2. There are no event triggers for the presentation mode, yet.
    • You can see about the triggers at here.
  3. When you run the script, the page that you are opening becomes the current page. It's not other user's page. When the script is run, the page that you are opening is retrieved.
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Yes, I meant to say slide 4. I do not want a comparison, I want currentPage to equal currentPresentationSlide. –  Jan 11 '19 at 02:00
  • @AwesomeTech10 I'm really sorry for the inconvenience. Before I update my answer, can I ask you about ``I want currentPage to equal currentPresentationSlide.``? You want to move to ``currentPresentationSlide``, when ``currentPage`` is not ``currentPresentationSlide``. Is my understanding correct? – Tanaike Jan 11 '19 at 02:08
  • No, I want to see if the user is presenting currentPresentationSlide –  Jan 11 '19 at 02:37
  • @AwesomeTech10 I'm sorry for my poor English skill. I cannot understand about what you want to do from your comments and question. Do you want to retrieve the current page of user by running the script by you, when users are presenting by the presentation mode? – Tanaike Jan 11 '19 at 02:51
  • Yes, I want that. –  Jan 11 '19 at 03:25
  • @AwesomeTech10 I updated my answer. Could you please confirm it? I apologize that what you want to do cannot be achieved. – Tanaike Jan 11 '19 at 05:13
  • Thank you. Is it possible to get the current page if user is not in presentation mode? –  Jan 13 '19 at 01:00
  • @AwesomeTech10 It cannot be directly achieved by the reason of 3rd point. But I think 2 workarounds. 1. Retrieve the current page by running the script by user. 2. Retrieve the current page of user by using Javascript on the sidebar. I apologize for my unclear answer. – Tanaike Jan 13 '19 at 01:11
  • I could have user run the script. How would I get the current page that way? –  Jan 16 '19 at 02:04
  • @AwesomeTech10 I apologize for my incomplete comment. In the current stage, Slides has no installable trigger for ``onOpen()`` yet. So at first, users have to run a function at script editor and authorize the scopes. And it makes users save the current page by the following flow. 1. Retrieve the current page using ``getSelection()``. 2. Save the current page using ``PropertiesService.getDocumentProperties()``. I think that page ID is suitable for this situation. 3. After user saved the current page, you can retrieve user's one. – Tanaike Jan 16 '19 at 04:53
0

To retrieve the current Page that the user is viewing, use the getSelection() and getCurrentPage() methods as follows:

var currentPage = SlidesApp.getActivePresentation().getSelection().getCurrentPage();

Note: this is an extract from the Google Apps Script documentation, that you can easily found in Google now. This SO post is also well-referenced in Google, but seems confusing. Consider looking at Google Apps Script documentation to learn more about what you can do.

Then, when you check if the user is on a particular slide, do the conditional check as follow:

var specialSlide = SlidesApp.getActivePresentation().getSlides()[3];
var currentPage = SlidesApp.getActivePresentation().getSelection().getCurrentPage();

if (currentPage.getObectId() === specialSlide.getObjectId()) {
  // User is on a particular page
}
Frenchcooc
  • 910
  • 6
  • 20
0

I have a similar request and I want to insert the current date and time to the speaker notes with just a click.

Here is the workable script for your reference.

function addCurrentDateTimeToTheNotes() {
  var now = new Date();

  var selection = SlidesApp.getActivePresentation().getSelection();
  var selectionType = selection.getSelectionType();

  // Logger.log('selectionType: ' + selectionType);

  var currentPage;
  var currentSlide;
  var speakerNotesShape;
  var speakerNotesText;
  if (selectionType == SlidesApp.SelectionType.CURRENT_PAGE) {
    currentPage = selection.getCurrentPage();
  }else if (selectionType == SlidesApp.SelectionType.PAGE) {
    var pageRange = selection.getPageRange();
    currentPage = pageRange.getPages()[0];
  }else if (selectionType == SlidesApp.SelectionType.PAGE_ELEMENT) {
    currentPage = selection.getCurrentPage();
  }else if (selectionType == SlidesApp.SelectionType.TEXT) {
    currentPage = selection.getCurrentPage();
  }

  if (currentPage != null) {
    currentSlide = currentPage.asSlide();
    speakerNotesShape = currentSlide.getNotesPage().getSpeakerNotesShape();
    speakerNotesTextRange = currentSlide.getNotesPage().getSpeakerNotesShape().getText();
    speakerNotesText = speakerNotesTextRange.asRenderedString();

    if( !speakerNotesText || speakerNotesText.trim() === '' ){
      speakerNotesTextRange.setText('Created at: \r\n' + now);
    }else{
      speakerNotesTextRange.setText('Modified at: \r\n' + now + '\r\n\r\n' + speakerNotesText);
    }
  }
}
Cray Kao
  • 573
  • 1
  • 8
  • 20