Mis-matched master & layout titles

50 views
Skip to first unread message

James Robinson

unread,
May 10, 2024, 6:17:28 PMMay 10
to Google Apps Script Community
I'm working on a script to auto generate some slides and want to be able to query the names of masters or layouts.

I currently have a deck with 3 layouts called:
  • layout1
  • layout2
  • layout3
However my script
var deck = SlidesApp.openById("############")
var layouts = deck.getLayouts()
Logger.log(layouts[0].getLayoutName())
var layoutsByName = {}
for (i = 0; i < layouts.length; i++){
Logger.log(layouts[i].getLayoutName())
}

produces
10:38:21 PM
Info
TITLE
10:38:21 PM
Info
TITLE_1
10:38:21 PM
Info
TITLE_1_1


Any help would be appreciated

Tanaike

unread,
May 12, 2024, 12:59:37 AMMay 12
to Google Apps Script Community
I guessed that in your situation, the reason for your current issue is due to that the display name of the layout you gave is different from the name retrieved by "getLayoutName()". In this case, in order to search the layouts with the display names, it is required to use Slides API. When this is reflected in your script, it becomes as follows.

Before you test this, please enable Slides API at Advanced Google services.

function myFunction() {
  // Please set your layout name you gave.
  var layoutDisplayName = ["layout1", "layout2", "layout3"];

  var deck = SlidesApp.getActivePresentation();
  var { layouts } = Slides.Presentations.get(deck.getId());
  var obj = new Map(layouts.map(({ layoutProperties: { name, displayName } }) => [name, displayName]));
  var layouts = deck.getLayouts();
  for (i = 0; i < layouts.length; i++) {
    var layout = layouts[i];
    var name = layout.getLayoutName();
    var displayname = obj.get(name);
    if (layoutDisplayName.includes(displayname)) {
      console.log(displayname);

      // If you want to use the layout object, you can use `layout` here.

    }
  }
}

When this script is run, the layout objects of layoutDisplayName are retrieved.

DME

unread,
May 23, 2024, 4:22:56 AMMay 23
to google-apps-sc...@googlegroups.com
t seems like the script is retrieving the names of the slide layouts, but they don't match the names you provided. This could happen if the layouts have been renamed or if there are other layouts in the deck that you're not expecting.

To address this issue, you can filter the layouts based on their names. Here's how you can modify your script to achieve that:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
var deck = SlidesApp.openById("############");
var layouts = deck.getLayouts();
var layoutNames = ["layout1", "layout2", "layout3"]; // Names of the layouts you expect

var layoutsByName = {};
for (var i = 0; i < layouts.length; i++) {
  var layoutName = layouts[i].getLayoutName();
  if (layoutNames.includes(layoutName)) {
    layoutsByName[layoutName] = layouts[i];
  }
}

// Now layoutsByName object will contain the layouts you expect, accessible by their names
for (var name in layoutsByName) {
  Logger.log("Layout Name: " + name);
}
---------------------------------------------------------------------------------------------------------------------------------------------------
This script will only store the layouts whose names match the names you provided ("layout1", "layout2", "layout3") in the layoutsByName object. You can then access these layouts using their names. Adjust the layoutNames array if you have different names or more layouts to include.

Thanks & Regards/-
Deepak Bansode 
Nagpur


--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/3ecdba00-3afa-4925-82a8-502842dc805dn%40googlegroups.com.


--
Reply all
Reply to author
Forward
0 new messages