How can I retrieve page IDs from the Figma API and utilize them as object IDs in Google Slides?

Challenge: I have received response.data from the Figma API, and my objective is to extract the “id” values from the nested structure. I am creating a Google Apps Script plugin that should generate slides in Google Slides for every page in Figma, each associated with their corresponding IDs.

Extracting Figma data:

var data = {
    "file": {
      "pages": [{
            "id": "1:2",
            "name": "Homepage",
            "type": "CANVAS"
          }]
    }
}

var pages = data.file.pages;
var pagesArray = [];
for (var index in pages) {
  pagesArray.push(pages[index].id);
}

var pageIDs = pagesArray;

Generating slides with Google Apps Script:

function createSlide(presentationId) {
  var slideId = JSON.stringify(pageIDs[0]);
  
  var requests = [{
    'createSlide': {
      'objectId': slideId,
      'insertionIndex': 0,
      'slideLayoutReference': {
        'predefinedLayout': 'BLANK'
      }
    }
  }];

  var response = Slides.Presentations.batchUpdate({ 'requests': requests }, presentationId);
  console.log('Slide created with ID: ' + response.replies[0].createSlide.objectId);
}

I tried using the push method to retrieve only the ID values but faced multiple errors. I’m not quite sure which method to use.

Additionally, I attempted to filter the IDs with the pageIDs.filter() method, which ran but created issues in my array manipulations, and I’m still unable to pass the slideId into my slide creation variable.

The Google Apps Script error I’m encountering is:

TypeError: Cannot call method “push” of undefined at function2(script:45)

The push error usually means pagesArray isn’t initialized properly or you’ve got a scope problem. Don’t use for-in loops with arrays - they’re buggy. Stick with regular for loops or map instead. Make sure you’re declaring pagesArray in the same scope where you’re trying to push to it. For Google Slides, your objectId needs to be a clean string without special characters, but skip JSON.stringify since that adds unwanted quotes. Try var slideId = 'slide_' + pageIDs[0].replace(/[^a-zA-Z0-9]/g, '_'); to make sure the ID works with Google Slides API.

your code logic looks fine, but google slides objectIds can’t have special charcters like colons. replace the colon in your figma id before using it as slideId. try var slideId = pageIDs[0].replace(':', '_'); - it’ll work better than JSON.stringify.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.