javascript - 使用 Google Docs API 查找表开始位置索引

标签 javascript google-docs-api

我想通过 Docs API 发出 InsertTableRow 请求,它需要一个 TableStartLocation。 我用过

var tableName = table[0];
foreach (var element in document.Body.Content)
if (element.Table != null)
{
   var checkTable = element.Table.TableRows[0].TableCells[0].Content[0].Paragraph.Elements[0]
                            .TextRun.Content.TrimEnd('\n'); //Get Text Value in first cell of the table
                        
   if (tableName.Equals(checkTable)) // Check if the table is the table that I want to add rows
      {
          Console.WriteLine("Add Table Row");
          TableUpdateRequest(ref requests, table, element.StartIndex); // Using element(StructuralElement) to get StartIndex
          break;
      }
}

要查找文档中的所有表格并尝试使用 element.StartIndex 作为表格起始位置,但我得到:Google.GoogleApiException : Google.Apis.Requests.RequestError Invalid requests[ 5].insertTableRow:无效的表开始位置。必须指定表的起始索引。 [400]

Table Start Location 的合适索引是什么?

最佳答案

tableStartLocation 是识别正确表所必需的

检索它的方法是例如与 documents.get .要缩小结果范围,您可以指定 fields,例如正文/内容(startIndex,表格)

这将返回给你一个类型的资源

{
  "body": {
    "content": [
      {},
      {
        "startIndex": 1
      },
      {
        "startIndex": 2,
        "table": {
          "rows": 4,
          "columns": 3,
          "tableRows": [
            {
             ...

换句话说:您现在知道您的 tableStartLocation2 - 与表的 startIndex 相同。

示例

  var resource = { "requests": [
    {
      "insertTableRow": {
        "tableCellLocation": {
          "tableStartLocation": {
            "index": 2
          }
        },
        "insertBelow": false
      }
    }
  ]
                 }
  Docs.Documents.batchUpdate(resource, documentId);

现在,根据您的文档,您可能有多个表,并且可能想要比较名称等,然后再决定哪个是正确 able 的起始索引。

关于javascript - 使用 Google Docs API 查找表开始位置索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62768028/

相关文章:

google-docs - Dart 应用程序如何从谷歌电子表格中读取单元格数据

javascript - 如何使用javascript从用户定义的excel文件中获取输入

javascript - 如何将 Canvas 显示保存为透明 PNG?

javascript - touchstart 和 touchend 事件引用同一元素,而事件在不同元素上完成

google-apps-script - Google应用程序脚本replaceText仅替换第一次出现的匹配字符串

python - 使用 Python 从 Google Drive/Workspace 下载电子表格

Python GAE 上传到 Google Docs

java - 无法通过 java api 将图像文件上传到 Google Doc

javascript - XMLParser 给出错误 '' 无法设置未定义的属性值''

javascript - Google Apps 脚本中的 Dropbox API v2/删除