Search code examples
javascriptjsonmultidimensional-arraygoogle-apps-scriptfigma-api

I am trying to find the page id of a page in Figma and map it to the object id in Google Slides


Problem: I got args.JSON from Figma API and I am trying to pull only the string “id” from the child array. I’m using google app script and Figma API to create a plugin. I want all the pages in Figma with their respective id to appear in google app script’s slides.

Reference: how I got the Pages Array from Figma API

Figma API

Figma Pagenode

Google App Script Advanced Slides Service

Figma page conversion:

var children = args = {
    "document": {
      "children": [{
            "id": "0:1",
            "name": "Layouts",
            "type": "CANVAS",
                          }]}}

var page = args.document.children;
JSON.page = [];
for (var i in page) {
  JSON.pages.unshift([1]);
};

var pageId = args.page.id;
idJSON.page.id = [];
for (var i in id) {
  idJSON.id, push(pages[i].("id"))
};

Google App Script Slide Create:

function createSlide(presentationId) {
  // You can specify the ID to use for the slide, as long as it's unique.
  var pageid = page.stringify(id[0])

  var requests = [{
    'createSlide': {
      'objectId': {
        'insertionIndex': {
          'slideLayoutReference': {
            'predefinedLayout': 'TITLE_AND_TWO_COLUMNS'
          }
        }
      }
    }
  }];

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

I tried using the shift method to get only the id but it ran an error I’m not sure what method to use.

I tried filtering all the id out of the page using idJSON.id,filter(("id")) and it ran but iit cause ab error on my JSON.pages.unshift([1]); and I still haven't been able to pass pageID into my var = slide.

I also tried running my variable PageID within and function and it worked as well however I still haven't been able to pass pageID into my var = slide. idJSON.id,filter(function(pageId){})

Error (in Google app script):

TypeError: Cannot call method "unshift" of undefined.
at fuctionn3(filename:53)


Solution

  • So what you want to turn var2 into a variable to capture the ID from var1. to unshift the string from the var1's array. then you want to use the concate with var 2 and var 3 give your slide an id.

    Here is the code

    function strings() {
      var var2 = {};
      var var1 = args.document.children;
      pageJSON.page = [];
      for (var i in page) {
        pageJSON.page.push(page[1].id);
      }
    
      function createPresentation(NewSlides, Slide) {
        var NewSlides = pageJSON.concat(Slide);
        var presentation = Slides.Presentations.create({
          title: "MyNewPresentation"
        });
        Logger.log("Created presentation with ID: " + presentation.s);
    
        function createSlide(presentationId, NewSlides) {
          // You can specify the ID to use for the slide, as long as it's unique.
          var pageid = page.stringify(id[0]);
    
          var requests = [
            {
              createSlide: {
                objectId: {
                  insertionIndex: {
                    slideLayoutReference: {
                      predefinedLayout: "TITLE_AND_TWO_COLUMNS"
                    }
                  }
                }
              }
            }
          ];
          var var3 = Slides.Presentations.batchUpdate(
            { requests: requests },
            presentationId(NewSlides)
          );
          Logger.log(
            "Created Slide with ID: " + slide.replies[0].createSlide.objectId
          );
        }
      }
    }