0

I have the following method that adds formatted text to a data structure for it to be used in a batchUpdate request:

   def add_text(
        self,
        text,
        bold=None,
        color=None,
        font_size=None,
        bg_color=None,
        italic=None,
    ):
        if text == "":
            return
        else:
            # Force to string to further processing
            text = str(text)
        style_updates = {
            "updateTextStyle": {
                "range": {
                    "startIndex": self.index,
                    "endIndex": self.index + len(text),
                },
                "textStyle": {
                    "bold": False,
                    "italic": False,
                    "foregroundColor": {
                        "color": {
                            "rgbColor": {
                                "red": 0,
                                "green": 0,
                                "blue": 0,
                            }
                        }
                    },
                    "backgroundColor": {},
                },
                "fields": "bold, italic, foregroundColor, backgroundColor",
            }
        }

        if bold:
            style_updates["updateTextStyle"]["textStyle"]["bold"] = bold

        if italic:
            style_updates["updateTextStyle"]["textStyle"]["italic"] = italic

        if color:
            style_updates["updateTextStyle"]["textStyle"][
                "foregroundColor"
            ] = {
                "color": {
                    "rgbColor": {
                        "red": color[0],
                        "green": color[1],
                        "blue": color[2],
                    }
                }
            }
        if font_size:
            style_updates["updateTextStyle"]["textStyle"]["fontSize"] = {
                "magnitude": font_size,
                "unit": "PT",
            }
            style_updates["updateTextStyle"]["fields"] += ", fontSize"

        if bg_color:
            style_updates["updateTextStyle"]["textStyle"][
                "backgroundColor"
            ] = {
                "color": {
                    "rgbColor": {
                        "red": bg_color[0],
                        "green": bg_color[1],
                        "blue": bg_color[2],
                    }
                }
            }

        self.updates += [
            {
                "insertText": {
                    "location": {
                        "index": self.index,
                    },
                    "text": text,
                }
            },
            style_updates,
        ]
        self.index += len(text)

The problem I'm having is that the resulting text in the target GDoc is "Heading 6" styled. I don't know where that style spec came from (the previous paragraph is "Heading 5", so it is not a hanging context that was taken). I also tried to append a paragraph style:

        if style:
            style_updates["updateParagraphStyle"] = {
                "range": {
                    "startIndex": self.index,
                    "endIndex": self.index + len(text),
                },
                "paragraphStyle": {"namedStyleType": style},
                "fields": "namedStyleType",
            }

But it raises a Google API exception saying that it os not allowed.

Any help would be appreciated.

1
  • 1
    Can you provide your sample input and output situations you expect using your script? Commented Jun 19 at 0:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.