错误:“ requests [0]”(oneof)处的值无效,其中一个字段“ kind”已设置。无法设置“ createShape”

时间:2018-11-04 01:46:52

标签: javascript node.js google-slides-api

我正在使用Google幻灯片API,并尝试根据用户输入的文本制作幻灯片。这是我的文字创建功能,

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 ();}});}

它会产生此错误:

  

错误:“ requests [0]”(oneof)处的值无效,其中一个字段“ kind”已设置。无法设置“ createShape”

文本已正确输入并存储。有没有人有办法解决吗?预先感谢。

1 个答案:

答案 0 :(得分:1)

此修改如何?

修改点:

  • createSlidecreateShapeinsertText放入requests数组中的每个元素。
  • insertionIndex
  • insertText从0开始。

修改后的脚本:

请如下修改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 - 1,
      text: txt,
    },
  }
];

注意:

  • 此修改后的脚本假定已启用Slides API,并且https://www.googleapis.com/auth/presentations的范围已包括在范围中。

参考文献:

如果这不是您想要的,对不起。