3

I am working with the google slides API and attempting to make a slide based on user inputted text. This is my text creating function,

var num = 1;
function createSlide(auth) {
    //AUTHENTICATION
    const slides = google.slides({version: 'v1', auth});

    //CHANGING VARS
    var slideId = 'slide_' + num;
    var pageId = slideId;;  
    var textId = 'text_box_' + num;
    var elementId = textId;
    var iIndex = num; 
    //SIZING
    var pt350 = {
        magnitude: 350,
        unit: 'PT',
    };   
    //ALL REQUESTS GO IN requests
    var requests = [{
        createSlide: {
            insertionIndex: iIndex,
            objectId: pageId,
            slideLayoutReference: {
                predefinedLayout: 'BLANK'
            }
        },
        //CREATE THE TEXTBOX
        createShape: {
          objectId: elementId,
          shapeType: 'TEXT_BOX',
          elementProperties: {
            pageObjectId: pageId,
            size: {
              height: pt350,
              width: pt350,
            },
            transform: {
              scaleX: 1,
              scaleY: 1,
              translateX: 350,
              translateY: 100,
              unit: 'PT',
            },
          },
        },
      },
      //INSERT TEXT
      {
        insertText: {
          objectId: elementId,
          insertionIndex: iIndex,
          text: txt,
        },
    }];
    //BATCH UPDATE
    return slides.presentations.batchUpdate({
        presentationId,
        resource: {
            requests,
        },
    }, (err, res) => {
        if (err) {
            error(err);
        }else{
            console.log('Success'); 
            //INCREASES COUNTER BY 1
            num = num + 1;
            //ASKS IF A NEW SLIDE WANTS TO BE CREATED
            askYOrN ();}});}

And it produces this error:

Error: Invalid value at 'requests[0]' (oneof), oneof field 'kind' is already set. Cannot set 'createShape'

The text is inputted and stored correctly. Does anyone have a solution? Thanks in advance.

Arkin Solomon
  • 592
  • 1
  • 5
  • 23

1 Answers1

2

How about this modification?

Modification points:

  • Put createSlide, createShape and insertText to each element in the array of requests.
  • insertionIndex at insertText starts from 0.

Modified script:

Please modify requests as follows.

var requests = [
  {
    createSlide: {
      insertionIndex: iIndex,
      objectId: pageId,
      slideLayoutReference: {
        predefinedLayout: 'BLANK',
      },
    },
  },
  {
    //CREATE THE TEXTBOX
    createShape: {
      objectId: elementId,
      shapeType: 'TEXT_BOX',
      elementProperties: {
        pageObjectId: pageId,
        size: {
          height: pt350,
          width: pt350,
        },
        transform: {
          scaleX: 1,
          scaleY: 1,
          translateX: 350,
          translateY: 100,
          unit: 'PT',
        },
      },
    },
  },
  {
    //INSERT TEXT
    insertText: {
      objectId: elementId,
      insertionIndex: iIndex - 1,
      text: txt,
    },
  }
];

Note:

  • This modified script supposes that Slides API is enabled and the scope of https://www.googleapis.com/auth/presentations is included in the scopes.

References:

If this was not what you want, I'm sorry.

Tanaike
  • 181,128
  • 11
  • 97
  • 165