Skip to content

Incremental engine for long horizon agents 🌟 Star if you like it!

fix(google_docs): keep bold, italic and code: set paragraph styles first (#2870)

Jiangzhoud50e52a1e336
diff --git a/dev/google_docs_real_account_check.py b/dev/google_docs_real_account_check.py
index 043a7582..39e94b3b 100644
--- a/dev/google_docs_real_account_check.py
+++ b/dev/google_docs_real_account_check.py
@@ -209,6 +209,23 @@ def main() -> int:
)
if leftover:
print(" left to write:", leftover[:5])
+ # Nothing left to write also holds for a block that reads back as a
+ # person's edit (the target never rewrites one), so check each block.
+ not_same = [
+ b.label
+ for b in blocks
+ if b.block_id not in state.ranges
+ or _docs.classify(
+ state, state.ranges[b.block_id], _markdown.fingerprint(b.paragraphs)
+ )
+ != "same"
+ ]
+ check(
+ "no block reads back as edited (styles and links round-trip)",
+ not not_same,
+ )
+ if not_same:
+ print(" read back as edited:", not_same)
_Counting.count = 0
app.update_blocking()
diff --git a/python/cocoindex/connectors/google_docs/_docs.py b/python/cocoindex/connectors/google_docs/_docs.py
index 42cdbde5..c14d54de 100644
--- a/python/cocoindex/connectors/google_docs/_docs.py
+++ b/python/cocoindex/connectors/google_docs/_docs.py
@@ -822,8 +822,10 @@ def _insert_requests(op: _Insert, inherits_bullet: bool) -> list[dict[str, Any]]
New paragraphs take the paragraph style of the paragraph they split and the
text style of the character before them, so every style is reset first and
- then set. A list item's nesting level is spelled as leading tabs, which
- ``createParagraphBullets`` reads and removes, so it comes last.
+ then set. Paragraph styles come before text styles: setting a paragraph's
+ named style clears the bold, italic and font of its text (links stay), as
+ the real API does. A list item's nesting level is spelled as leading tabs,
+ which ``createParagraphBullets`` reads and removes, so it comes last.
"""
paragraphs = [p for _, p in op.paragraphs]
texts = [
@@ -843,15 +845,36 @@ def _insert_requests(op: _Insert, inherits_bullet: bool) -> list[dict[str, Any]]
cursor += u16len(t) + 1
end = cursor
requests: list[dict[str, Any]] = [
- {"insertText": {"location": {"index": position}, "text": text}},
+ {"insertText": {"location": {"index": position}, "text": text}}
+ ]
+ index = 0
+ while index < len(paragraphs):
+ stop = index
+ while (
+ stop + 1 < len(paragraphs)
+ and paragraphs[stop + 1].style == paragraphs[index].style
+ ):
+ stop += 1
+ stop_end = starts[stop + 1] if stop + 1 < len(starts) else end
+ requests.append(
+ {
+ "updateParagraphStyle": {
+ "range": {"startIndex": starts[index], "endIndex": stop_end},
+ "paragraphStyle": {"namedStyleType": paragraphs[index].style},
+ "fields": "*",
+ }
+ }
+ )
+ index = stop + 1
+ requests.append(
{
"updateTextStyle": {
"range": {"startIndex": first, "endIndex": end},
"textStyle": {},
"fields": "*",
}
- },
- ]
+ }
+ )
for paragraph, start in zip(paragraphs, starts):
offset = start + (paragraph.bullet or 0)
for run in paragraph.runs:
@@ -879,25 +902,6 @@ def _insert_requests(op: _Insert, inherits_bullet: bool) -> list[dict[str, Any]]
}
}
)
- index = 0
- while index < len(paragraphs):
- stop = index
- while (
- stop + 1 < len(paragraphs)
- and paragraphs[stop + 1].style == paragraphs[index].style
- ):
- stop += 1
- stop_end = starts[stop + 1] if stop + 1 < len(starts) else end
- requests.append(
- {
- "updateParagraphStyle": {
- "range": {"startIndex": starts[index], "endIndex": stop_end},
- "paragraphStyle": {"namedStyleType": paragraphs[index].style},
- "fields": "*",
- }
- }
- )
- index = stop + 1
groups: list[tuple[int, int]] = []
index = 0
while index < len(paragraphs):
diff --git a/python/tests/connectors/test_google_docs_target.py b/python/tests/connectors/test_google_docs_target.py
index 8f2a2096..c14bee68 100644
--- a/python/tests/connectors/test_google_docs_target.py
+++ b/python/tests/connectors/test_google_docs_target.py
@@ -82,6 +82,12 @@ class BadRequest(Exception):
pass
+# The text properties setting a paragraph's named style clears, as measured on
+# the real API (2026-11-27): bold, italic and the font went, a link (with its
+# underline and color) stayed.
+_NAMED_STYLE_TEXT = ("bold", "italic", "weightedFontFamily", "fontSize")
+
+
def _code_units(text: str) -> list[str]:
raw = text.encode("utf-16-le", "surrogatepass")
return [
@@ -199,6 +205,12 @@ class FakeDoc:
else:
for name in body["fields"].split(","):
para["style"][name] = body["paragraphStyle"][name]
+ if body["fields"] == "*" or "namedStyleType" in body["fields"]:
+ # As Docs does: setting a named style clears the text
+ # properties a named style defines; a link stays.
+ for unit in self.units[s - 1 : e - 1]:
+ for name in _NAMED_STYLE_TEXT:
+ unit.style.pop(name, None)
elif kind == "deleteParagraphBullets":
self._check_range(start, end)
for s, e in self._overlapping(start, end):