Search code examples
google-slides-api

Centering google-slides text


I'm creating google slides from a csv upload via the browser. When I'm inserting text, how would I center that? They only show up on the right side for some reason.

              const requests = [
                {
                  createShape: {
                    objectId: `shape${csv.ID}`,
                    shapeType: "TEXT_BOX",
                    elementProperties: {
                      pageObjectId: `slide${index}`,
                      size: {
                        height: height,
                        width: width
                      },
                      transform: {
                        scaleX: 1,
                        scaleY: 1,
                        translateX: 350,
                        translateY: 100,
                        unit: "PT"
                      }
                    }
                  }
                },
                {
                  insertText: {
                    objectId: `shape${csv.ID}`,
                    insertionIndex: 0,
                    text: csv.text
                  }
                }
              ];
    ```

Solution

    • You want to put the value of csv.text to the center of the object of shape${csv.ID} using the method of batchUpdate in Slides API.

    If my understanding is correct, how about this modification? In this modification, UpdateParagraphStyleRequest is added to the request body of requests.

    Modified script:

    const requests = [
      {
        createShape: {
          objectId: `shape${csv.ID}`,
          shapeType: "TEXT_BOX",
          elementProperties: {
            pageObjectId: `slide${index}`,
            size: {
              height: height,
              width: width
            },
            transform: {
              scaleX: 1,
              scaleY: 1,
              translateX: 350,
              translateY: 100,
              unit: "PT"
            }
          }
        }
      },
      {
        insertText: {
          objectId: `shape${csv.ID}`,
          insertionIndex: 0,
          text: csv.text
        }
      },
      {
        updateParagraphStyle: {
          objectId: `shape${csv.ID}`,
          textRange: {type: "ALL"},
          style: {alignment: "CENTER"},
          fields: "alignment"
        }
      }
    ];
    

    Reference: