如何使用 Google Docs API 请求正确计算嵌套列表的索引?

问题描述 投票:0回答:1

这是我当前的请求对象,它给了我这个错误: “无效的请求[23].updateParagraphStyle:索引 66 必须小于引用段的结束索引 64。”,

A 或 得到 1 索引,任何其他字符得到 1。不知何故,我似乎偏离了 3,但我不明白为什么。 GPT 4 在这种事情上也很糟糕,似乎无法做基本的数学。

我猜我在某种程度上误解了索引,但我不知道哪里出了问题。如果删除段落样式部分,我可以让它大部分正常工作。感觉我的索引偏离了几个,所以我遇到了奇怪的问题,例如在有序列表情况下,段落仍然是列表的一部分。任何建议将不胜感激。

这是我的原文内容:

 * Bullet1
   * Bullet2
     * Bullet3
     * Bullet3b

Para1
 1. Ordered1
   1.1 Ordered2
      1.1.1 Ordered3

Para2

我正在将 TipTap JSON 转换为 google 文档并致力于支持嵌套列表,我的函数生成以下请求数组。

[
    {
        "insertText": {
            "location": {
                "index": 1
            },
            "text": "Bullet1"
        }
    },
    {
        "insertText": {
            "location": {
                "index": 8
            },
            "text": "\n"
        }
    },
    {
        "insertText": {
            "location": {
                "index": 9
            },
            "text": "\t"
        }
    },
    {
        "insertText": {
            "location": {
                "index": 10
            },
            "text": "Bullet2"
        }
    },
    {
        "insertText": {
            "location": {
                "index": 17
            },
            "text": "\n"
        }
    },
    {
        "insertText": {
            "location": {
                "index": 18
            },
            "text": "\t"
        }
    },
    {
        "insertText": {
            "location": {
                "index": 19
            },
            "text": "Bullet3"
        }
    },
    {
        "insertText": {
            "location": {
                "index": 26
            },
            "text": "\n"
        }
    },
    {
        "insertText": {
            "location": {
                "index": 27
            },
            "text": "Para1"
        }
    },
    {
        "insertText": {
            "location": {
                "index": 32
            },
            "text": "\n"
        }
    },
    {
        "insertText": {
            "location": {
                "index": 33
            },
            "text": "Ordered1"
        }
    },
    {
        "insertText": {
            "location": {
                "index": 41
            },
            "text": "\n"
        }
    },
    {
        "insertText": {
            "location": {
                "index": 42
            },
            "text": "\t"
        }
    },
    {
        "insertText": {
            "location": {
                "index": 43
            },
            "text": "Ordered2"
        }
    },
    {
        "insertText": {
            "location": {
                "index": 51
            },
            "text": "\n"
        }
    },
    {
        "insertText": {
            "location": {
                "index": 52
            },
            "text": "\t"
        }
    },
    {
        "insertText": {
            "location": {
                "index": 53
            },
            "text": "Ordered3"
        }
    },
    {
        "insertText": {
            "location": {
                "index": 61
            },
            "text": "\n"
        }
    },
    {
        "insertText": {
            "location": {
                "index": 62
            },
            "text": "Para2"
        }
    },
    {
        "createParagraphBullets": {
            "range": {
                "startIndex": 1,
                "endIndex": 27
            },
            "bulletPreset": "BULLET_DISC_CIRCLE_SQUARE"
        }
    },
    {
        "updateParagraphStyle": {
            "range": {
                "startIndex": 27,
                "endIndex": 32
            },
            "paragraphStyle": {
                "namedStyleType": "NORMAL_TEXT",
                "direction": "LEFT_TO_RIGHT",
                "alignment": "START",
                "lineSpacing": 115,
                "spaceAbove": {
                    "magnitude": 4,
                    "unit": "PT"
                },
                "spaceBelow": {
                    "magnitude": 4,
                    "unit": "PT"
                },
                "indentFirstLine": {
                    "magnitude": 0,
                    "unit": "PT"
                }
            },
            "fields": "alignment,lineSpacing,spaceAbove,spaceBelow,indentFirstLine,namedStyleType"
        }
    },
    {
        "createParagraphBullets": {
            "range": {
                "startIndex": 33,
                "endIndex": 62
            },
            "bulletPreset": "NUMBERED_DECIMAL_NESTED"
        }
    },
    {
        "updateParagraphStyle": {
            "range": {
                "startIndex": 62,
                "endIndex": 67
            },
            "paragraphStyle": {
                "namedStyleType": "NORMAL_TEXT",
                "direction": "LEFT_TO_RIGHT",
                "alignment": "START",
                "lineSpacing": 115,
                "spaceAbove": {
                    "magnitude": 4,
                    "unit": "PT"
                },
                "spaceBelow": {
                    "magnitude": 4,
                    "unit": "PT"
                },
                "indentFirstLine": {
                    "magnitude": 0,
                    "unit": "PT"
                }
            },
            "fields": "alignment,lineSpacing,spaceAbove,spaceBelow,indentFirstLine,namedStyleType"
        }
    }
]
google-docs-api
1个回答
0
投票

从你显示的输出情况来看,下面的请求体怎么样?

修改后的请求体:

[
  {
    "insertText": {
      "location": {
        "index": 1
      },
      "text": "Bullet1"
    }
  },
  {
    "insertText": {
      "location": {
        "index": 8
      },
      "text": "\n"
    }
  },
  {
    "insertText": {
      "location": {
        "index": 9
      },
      "text": "\t"
    }
  },
  {
    "insertText": {
      "location": {
        "index": 10
      },
      "text": "Bullet2"
    }
  },
  {
    "insertText": {
      "location": {
        "index": 17
      },
      "text": "\n"
    }
  },
  {
    "insertText": {
      "location": {
        "index": 18
      },
      "text": "\t"
    }
  },
  {
    "insertText": {
      "location": {
        "index": 19
      },
      "text": "Bullet3"
    }
  },
  {
    "insertText": {
      "location": {
        "index": 26
      },
      "text": "\n"
    }
  },
  {
    "insertText": {
      "location": {
        "index": 27
      },
      "text": "Para1"
    }
  },
  {
    "insertText": {
      "location": {
        "index": 32
      },
      "text": "\n"
    }
  },
  {
    "insertText": {
      "location": {
        "index": 33
      },
      "text": "Ordered1"
    }
  },
  {
    "insertText": {
      "location": {
        "index": 41
      },
      "text": "\n"
    }
  },
  {
    "insertText": {
      "location": {
        "index": 42
      },
      "text": "\t"
    }
  },
  {
    "insertText": {
      "location": {
        "index": 43
      },
      "text": "Ordered2"
    }
  },
  {
    "insertText": {
      "location": {
        "index": 51
      },
      "text": "\n"
    }
  },
  {
    "insertText": {
      "location": {
        "index": 52
      },
      "text": "\t\t"
    }
  },
  {
    "insertText": {
      "location": {
        "index": 54
      },
      "text": "Ordered3"
    }
  },
  {
    "insertText": {
      "location": {
        "index": 62
      },
      "text": "\n"
    }
  },
  {
    "insertText": {
      "location": {
        "index": 63
      },
      "text": "Para2"
    }
  },
  {
    "createParagraphBullets": {
      "range": {
        "startIndex": 1,
        "endIndex": 27
      },
      "bulletPreset": "BULLET_DISC_CIRCLE_SQUARE"
    }
  },
  {
    "updateParagraphStyle": {
      "range": {
        "startIndex": 27,
        "endIndex": 32
      },
      "paragraphStyle": {
        "namedStyleType": "NORMAL_TEXT",
        "direction": "LEFT_TO_RIGHT",
        "alignment": "START",
        "lineSpacing": 115,
        "spaceAbove": {
          "magnitude": 4,
          "unit": "PT"
        },
        "spaceBelow": {
          "magnitude": 4,
          "unit": "PT"
        },
        "indentFirstLine": {
          "magnitude": 0,
          "unit": "PT"
        }
      },
      "fields": "alignment,lineSpacing,spaceAbove,spaceBelow,indentFirstLine,namedStyleType"
    }
  },
  {
    "createParagraphBullets": {
      "range": {
        "startIndex": 33,
        "endIndex": 58
      },
      "bulletPreset": "NUMBERED_DECIMAL_NESTED"
    }
  },
  {
    "updateParagraphStyle": {
      "range": {
        "startIndex": 58,
        "endIndex": 64
      },
      "paragraphStyle": {
        "namedStyleType": "NORMAL_TEXT",
        "direction": "LEFT_TO_RIGHT",
        "alignment": "START",
        "lineSpacing": 115,
        "spaceAbove": {
          "magnitude": 4,
          "unit": "PT"
        },
        "spaceBelow": {
          "magnitude": 4,
          "unit": "PT"
        },
        "indentFirstLine": {
          "magnitude": 0,
          "unit": "PT"
        }
      },
      "fields": "alignment,lineSpacing,spaceAbove,spaceBelow,indentFirstLine,namedStyleType"
    }
  }
]

测试:

当此请求正文与Google Docs API 的batchUpdate方法一起使用时,将获得以下结果。

参考:

© www.soinside.com 2019 - 2024. All rights reserved.