n179911
n179911

Reputation: 20341

Google Slide API how to replace text 'Click to Add Text'

After I use Google Slide API to create slides for me, I see text box with text saying 'Click to add Text' , 'Click to add title', And same for 2 column sides, how can i set the text for the right 'Click to add Text', and same for the left.

How can I programmatically find out those text boxes and set the text I want?

Here is the code: I 1) create a slide and make it TITLE_AND_TWO_COLUMNS layout 2) create a Shape and 3) Insert Text to the Shape. But when I view the slide in google drive, i see 'Click to Add Text'

        IList<Request> requests = new List<Request>();
        String slideId = "MyNewSlide_001";
        requests.Add(new Request()
        {
            CreateSlide = new CreateSlideRequest()
            {
                ObjectId = slideId,
                InsertionIndex = 1,
                SlideLayoutReference = new LayoutReference()
                {
                    PredefinedLayout = "TITLE_AND_TWO_COLUMNS"
                }
            }
        });

        String textBoxId = "MyTextBox_01";
        Dimension pt350 = new Dimension()
        {
            Magnitude = 350.0,
            Unit = "PT",
        };
        requests.Add(new Request()
        {
            CreateShape = new CreateShapeRequest()
            {
                ObjectId = textBoxId,
                ShapeType = "TEXT_BOX",
                ElementProperties = new PageElementProperties()
                {
                    PageObjectId = slideId,
                    Size = new Size()
                    {
                        Height = pt350,
                        Width = pt350
                    },
                },
            }
        });

        requests.Add(new Request()
        {
            UpdateShapeProperties = new UpdateShapePropertiesRequest()
            {
                ObjectId = textBoxId,
                ShapeProperties = new ShapeProperties
                {
                    ShapeBackgroundFill = new ShapeBackgroundFill
                    {
                        SolidFill = new SolidFill
                        {
                            Color = new OpaqueColor
                            {
                                ThemeColor = "HYPERLINK"
                            }
                        }
                    }
                },
                Fields = "shapeBackgroundFill.solidFill.color,outline"
            },
        });

        // Insert text into the box, using the object ID given to it.
        requests.Add(new Request()
        {
            InsertText = new InsertTextRequest()
            {
                ObjectId = textBoxId,
                InsertionIndex = 0,
                Text = "New Box Text Inserted"
            }
        });

Upvotes: 1

Views: 4763

Answers (1)

Maurice Codik
Maurice Codik

Reputation: 598

Those boxes with "Click to add text" text are placeholder Shapes that are automatically copied onto your slide from the layout. That text is only visible in the editor: they wont have any text in present mode, unless you insert text into them directly.

You can insert text into them just like any other shapes in the Slides API.

  • Read the page or presentation with one of the GET APIs (presentations.get or presentations.pages.get)
  • Find the object IDs of the placeholders you want to write in. You can identify which is which looking at the pageElement.shape.placeholder message on each page element on your slide. You want the one with type = TITLE for the title, and so on.
  • Call batchUpdate with an insertText request to add the text, just like you're already doing in your code

Some of this is covered in the Edit text in a specified shape in the documentation.

Upvotes: 2

Related Questions