【问题标题】:Why is the equation object empty in json when returned from the google doc api?从google doc api返回时,为什么json中的方程对象为空?
【发布时间】:2021-04-01 11:40:55
【问题描述】:

我正在尝试使用 google doc api 获取 google doc 内容,但我发现从 google doc api 返回的 json 缺少 eqaution 信息。

示例内容:- google doc excerpt 示例请求:-

const baseUrl = `https://docs.googleapis.com/v1/documents/${doc_id}`
    const response = await googleReq(baseUrl, ctx.accessToken).then(res => {
        let data = res.json()
        return data
    }).catch(err => console.log(err))

const googleReq = (url, token) => {
    if (!token) return new Error("No accessToken")
    return fetch(url, {
        method: 'GET',
        headers: {
            Authorization: `Bearer ${token}`
        }
    })
}

example response :- 
...
{
   "startIndex": 321,
   "endIndex": 330,
   "equation": {}
}
...

如您所见,方程块没有返回任何内容。我不知道为什么会这样。

【问题讨论】:

    标签: node.js google-docs google-docs-api


    【解决方案1】:

    答案:

    很遗憾,目前您无法从 Google Docs API 获取方程式信息。

    更多信息:

    根据developer documentation,API 返回的equation 块应包含:

    • suggestedInsertionIds[]
    • suggestedDeletionIds[]

    这些 ID 是字母数字字符串,表示编辑器将插入或删除的建议。

    此块将不包含任何公式文本或其他相关信息。

    唯一的方法:

    目前只有一种以编程方式获取方程文本,即使用 Google Apps Script 方法。

    执行此操作的示例脚本是:

    // Copyright 2021 Google LLC.
    // SPDX-License-Identifier: Apache-2.0
    
    function getEquations() {
    
      const searchElementType = DocumentApp.ElementType.EQUATION
      const documentBody = DocumentApp.getActiveDocument().getBody()
      const searchResult = null
    
      while (searchResult = documentBody.findElement(searchElementType,searchResult)) {
        let par = searchResult.getElement().asEquation()
        console.log("Search Result : " + par.getText())
      }
    }
    

    作为一种解决方法,您可以将其部署为一个网络应用程序,将公式文本作为 JSON 内容提供,然后使用 JavaScript 中的 fetch API 来获取此数据,而不是直接使用 Google Docs API。

    功能要求:

    但是,您可以让 Google 知道这是一项对于访问他们的 API 很重要的功能,并且您想请求他们实施它。

    Google 的Issue Tracker 是开发人员报告问题并为其开发服务提出功能请求的地方,我强烈建议您在那里提出功能请求。提交此文件的最佳组件是Google Docs component,带有Feature Request 模板。

    参考资料:

    基于 Web 应用的 Workround 参考资料:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多