Non-Editable Timestamps in Google Slides based on creation of a new slide in a presentation

324 views Asked by At

This is probably a longshot, but I'm a teacher working with students on Engineering Design notebooks. One of the main goals/requirements is that each page has a date, and that date shouldn't be editable by students.

I've been looking for scripts that allow me to automate the date in Google Slides, which is where we are building our notebook template.

There are quite a few scripts for updating a date whenever you open a presentation.

does anyone know how to add a script that would update the current date in a text field, only when a new slide is created? EX: Student opens their digital google slides notebook, then they click CTRL+M or right click > New Slide. When the new slide pops up, it autopopulates the date field with the current date, and cannot be edited by the student.

Thanks for any help in pointing me in the right direction.

1

There are 1 answers

0
ecle On

Yes, you can create a non-editable textbox and later fill it with a timestamp using a script.

The solution below may not meet your whole question, maybe just the first goal, but the idea can be converted into a better script.

A textbox cannot be edited in the slide view if it is placed in a layout in the master view.

To give you the idea, we are going to create a "non-editable" textbox in the main slide layout as an example:

  1. We need to get the Slide id first. From our Google Slides document, take note of its id from its URL format https://docs.google.com/presentation/d/{your slide id}/edit. Use {your slide id} to define the constant SLIDE_ID in your script.
  2. To edit the main layout, we have to enable the Master view from the menu View > Master
  3. In the Master view, we will notice the slides under the "Master" and "Layouts" list. Choose the first layout from the "Layouts" list. The first layout is usually the main slide layout.
  4. In the main slide layout, we create a text box with the text Date. We use the text to find the textbox object id with the following Script 1 first. In the image below, it is the red textbox with the Date text.

enter image description here

Script 1:

    const SLIDE_ID = "{your slide id}";

    //don't enable the debug log when running this script in production to speed up the processing
    const ENABLE_DEBUG_LOG = true;
    
    function findLayoutObjectIdByText_(text) {
      var presentation = SlidesApp.openById(SLIDE_ID);
      var layouts = presentation.getLayouts(); //layouts[0].getShapes()[0].getObjectId()
      layouts.forEach(layout => { // iterate through every layout in the presentation
        layout.getShapes().forEach(shape => { // iterate through every shape in the slide
          const textStr = shape.getText().asString(); // get shape text string
          const textObject = shape; // get shape text object
          const matches = textStr.match(text); // find matches
          if (matches != null) {
            if (ENABLE_DEBUG_LOG) Logger.log("TEXT : " + textStr);
            if (ENABLE_DEBUG_LOG) Logger.log("MATCH : " + textObject.getObjectId());
            return; //a workaround since we can't use a break statement in forEach() loop 
          }
        });
      });
    }

    function openSlide() {

      findLayoutObjectIdByText_(/^Date/g);
      
    }
  1. Select Run > Debug > openSlide. After a few seconds from your script editor, go to View > Logs to see the output for "MATCH". For example, the object id is ga66cd6addf_0_17

Output:

[20-11-01 12:21:09:042 HKT] TEXT : Date

[20-11-01 12:21:09:044 HKT] MATCH : ga66cd6addf_0_17
  1. We can now modify the codes in the openSlide() function in Script 1 as follows:

Modified Script 1:

    function openSlide() {

      //findLayoutObjectIdByText_(/^Date/g); //comment it out once we are done
      
      var presentation = SlidesApp.openById(SLIDE_ID);
      var today = new Date();
      var shape = presentation.getPageElementById("ga66cd6addf_0_17").asShape();
      if (shape ! null) { //if the shape object present (not being removed)
        shape.getText().setText(today.toLocaleDateString('en-US', {day: 'numeric', month: "short", year: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric'}));
      }
    }
  1. Select Run > Debug > openSlide. After a few seconds, we can see the Date in the textbox has been changed to a proper timestamp in our Google Slides document as follows:

enter image description here

Now, we can use the modified openSlide() function as a time-driven trigger (if the script scope is in the Google Slides document) or as an event trigger (if the script scope is in Google Form or Google Sheet) to keep updating the text. The timestamp textbox cannot be modified when we are not in the master view.

It is not necessary to create a new slide using a script, but you can refer to https://developers.google.com/slides/how-tos/create-slide.

The important thing is to make sure to edit every layout in the "Layouts" list in the Master view with the above idea. Every time the student creates a new slide from the Google Slides document, the new slide will automatically retrieve the assigned slide layout (most likely it is the third Layout in the Master view) that you have included in the timestamp textbox.

Next idea: the above steps can be turned into a whole script if you want to but I will not cover how it can be done here.

Caveat: Since every new slide uses the assigned default slide layout, we are going to see the same timestamp value for all newly created slides. Maybe it is better to run a script that is executed from a Google Form's onSubmit() scope trigger where the student submits his/her attendance for every session, for example. Once submitted, the script will duplicate a Google Slides document for the student from the master/template Google Slides document that has included the "Date" textbox in the layout and timestamps the "Date" textbox in the duplicated document (like the concept of mailmerge/document bookmarks in the Microsoft Word). The script will only act upon the duplicate but not the master/template. The link to this duplicated Google Slides document can be provided from a response email or from a Web redirect. You can include the student's name in this duplicate document as well! So, every student will have his/her own Google Slides document with a different current timestamp each time it is created.

See the merge concept in https://developers.google.com/slides/how-tos/merge

Note: we still cannot avoid the students from modifying the layouts in the Master view when we give them the edit permission.