Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 如何获得表格宽度_Google Apps Script_Google Docs_Grid Layout - Fatal编程技术网

Google apps script 如何获得表格宽度

Google apps script 如何获得表格宽度,google-apps-script,google-docs,grid-layout,Google Apps Script,Google Docs,Grid Layout,我想得到谷歌文档中表格的宽度。下面的屏幕截图显示了一个演示文档。它只有一个表,其中有一行只有一个单元格。通过单击Insert>table,然后单击插入一个具有单行和单个单元格的表的选项,可以添加该表。插入表格后未对其进行操作 在Google Apps脚本类表的类表中,它也没有获取宽度的方法,类行也没有,但是类单元格有getWidth(),所以我尝试使用它,但它返回null。我是文档所有者,它存储在我的单元中,我使用的是旧的运行时(Mozilla Rhino)、Chrome,一个G套件帐户。该项

我想得到谷歌文档中表格的宽度。下面的屏幕截图显示了一个演示文档。它只有一个表,其中有一行只有一个单元格。通过单击Insert>table,然后单击插入一个具有单行和单个单元格的表的选项,可以添加该表。插入表格后未对其进行操作

在Google Apps脚本类表的类表中,它也没有获取宽度的方法,类行也没有,但是类单元格有
getWidth()
,所以我尝试使用它,但它返回
null
。我是文档所有者,它存储在我的单元中,我使用的是旧的运行时(Mozilla Rhino)、Chrome,一个G套件帐户。该项目是通过单击工具>脚本编辑器从Docs UI创建的

以下是代码(项目不包括任何其他内容)

代码是从脚本编辑器运行的。这是日志

我已经在问题追踪器中搜索了getWidth。返回了一些结果,但没有一个结果与此相关

我已经找过了

虽然它看起来像是复制品,但它是关于插入图像的。另一方面,OP尝试了

var width = insertPlace.getParent().asParagraph().asTableCell().getWidth();
但这也不起作用。目前的答案是获取从Google Drive插入的图像的宽度

所以问题是如何获得桌子宽度?

如果
getWidth()
有问题,那么是否有其他方法不仅可以在表格使用默认宽度时找到表格宽度,还可以在表格较小时找到表格宽度?

这是一个记录

  • :
ja..。@google.com

状态:不会修复(预期行为)

你好,, 谢谢你的报告。如果单元格的宽度没有改变,函数“getWidth”返回null。您可以手动更改宽度或使用“设置宽度”方法使其适应图像大小。 更多信息:

  • :
du..。@google.com

状态:不会修复(预期行为)

尽管所有这些都没有被记录,新表的预期行为是为未设置的属性检索null。如果选择单元格并修改其格式(字体大小、斜体等),则将获得这些属性的值

对于单元格宽度,当创建新表时,所有单元格都是“均匀分布的单元格”,这意味着它没有“固定宽度”,如果修改单元格宽度,它将更改为固定宽度。如果修改整个表格的默认宽度,所有单元格将更改为固定宽度。GoogleDocAPI的预期行为是仅返回具有固定宽度的单元格的宽度

我实现了以下解决方法,它将记录每个单元格的宽度和表的总宽度(新表的默认宽度为450点)

解决方法:

/**
*代码由du..@google.com编写
*@见https://issuetracker.google.com/issues/143810853#comment2
*/
函数getTabeCellAttributes(){
var fileId='[FILE-ID]';
var textInTableCell=“测试”;
var body=DocumentApp.openById(fileId.getBody();
var tableRow=body.findText(textInTableCell).getElement().getParent().getParent().getParent().asTableRow();
var均匀分布单元=0;
var defaultTableWidth=450;
var totalWidth=0;

对于(var i=0;i,感谢@TheMaster提到这些问题(我无法找到它们)。下面是一个非常简单的方法,可以获得表的大小,该表适用于将表作为文档体子级的特定情况

function myFunction1() {
  const doc = DocumentApp.getActiveDocument();
  const body = doc.getBody();
  const attributes = body.getAttributes();
  const width = attributes[DocumentApp.Attribute.PAGE_WIDTH] - attributes[DocumentApp.Attribute.MARGIN_LEFT] - attributes[DocumentApp.Attribute.MARGIN_RIGHT];
  Logger.log(width);
}

我的默认页面大小为字母(宽度=8.5英寸),边距为1英寸


记录的表格宽度为468点(1英寸=72点)=6.5英寸,因此宽度是预期的。

我相信您的目标如下

  • 您希望使用Google Apps脚本从创建为默认大小的表中检索表宽
问题和解决方法: 不幸的是,在当前阶段,没有从Google Document service和Docs API中创建为默认大小的表中检索表宽的方法。这已经被和提到。因此在这个回答中,我想提出一个使用Google Apps脚本检索表宽的解决方法

在这个变通方法中,我使用Microsoft Word。这个变通方法的流程如下。在这个流程中,它假设已经创建了一个像您的问题一样的Google文档

  • 使用驱动API将Google文档(DOCX数据)转换为Microsoft Word
  • 使用谷歌应用程序脚本解析DOCX数据。
    • 当转换的DOCX数据解压后,可以将数据作为XML数据进行分析。幸运的是,在Microsoft Docs中,详细规范是以开放XML形式发布的。因此,在这种情况下,可以使用Google Apps脚本的XmlService来分析诸如XLSX、DOCX和PPTX等Microsoft文档。我认为这种方法对于其他情况也很有用
  • 从DOCX数据中检索表格宽度
  • 示例脚本: 结果:
    • 使用此脚本检索表时,将检索
      tableWidth:451.3
      。在这种情况下,单位为
      pt
    • 结果数组的索引表示表的索引。例如,Google文档中的第一个表是
      0
    注:
    • 作为对上述脚本的确认,当使用
      DocumentApp.getActiveDocument().getBody().appendTable([[“”]]).getCell(0,0).setWidth(200)的脚本创建表时
      并且使用上述示例脚本检索表,我可以确认检索到了
      tableWidth:200
      。从这种情况来看,Google文档中的表大小与DOCX数据中的表大小相同
    参考资料:

    因此,对于使用默认宽度的表格,唯一的方法是计算“正文”的宽度(页宽-左ma
    /**
     * Code written by du....@google.com
     * @see https://issuetracker.google.com/issues/143810853#comment2
     */
    function getTabeCellAttributes() {
      var fileId = '[FILE-ID]';
      var textInTableCell = "test";
      var body = DocumentApp.openById(fileId).getBody();  
    
      var tableRow = body.findText(textInTableCell).getElement().getParent().getParent().getParent().asTableRow();
      var evenlyDistributedCells = 0;
      var defaultTableWidth = 450;
      var totalWidth = 0;
     
      for(var i=0; i<tableRow.getNumChildren(); i++) {
        var tableCell = tableRow.getChild(i).asTableCell();
        var tableCellWidth = tableCell.getWidth();
       
        if(tableCellWidth == null) {
          evenlyDistributedCells++;
        }
        else {
          totalWidth += tableCellWidth;
          defaultTableWidth -= tableCellWidth;
          Logger.log("Width of cell number: " + (i+1) + " is: " + tableCellWidth)
        }
      }
     
      if (evenlyDistributedCells!=0) {
        var evenlyDistributedWidth = defaultTableWidth/evenlyDistributedCells;
        totalWidth += defaultTableWidth;
        Logger.log('There are ' + evenlyDistributedCells + ' evenly distributed cells with a width of: ' + evenlyDistributedWidth)
      }
      else {
        Logger.log("There are no evenly distributed cells, all cells have a fixed width.")
      }
     
      Logger.log('The total width of the table is: ' + totalWidth);
    }
    
    function myFunction1() {
      const doc = DocumentApp.getActiveDocument();
      const body = doc.getBody();
      const attributes = body.getAttributes();
      const width = attributes[DocumentApp.Attribute.PAGE_WIDTH] - attributes[DocumentApp.Attribute.MARGIN_LEFT] - attributes[DocumentApp.Attribute.MARGIN_RIGHT];
      Logger.log(width);
    }
    
    
    function myFunction() {
      const documentId = DocumentApp.getActiveDocument().getId();
      const url = "https://docs.google.com/feeds/download/documents/export/Export?exportFormat=docx&id=" + documentId;
      const blob = UrlFetchApp.fetch(url, {headers: {authorization: `Bearer ${ScriptApp.getOAuthToken()}`}}).getBlob().setContentType(MimeType.ZIP);
      const docx = Utilities.unzip(blob);
      const document = docx.filter(b => b.getName() == "word/document.xml");
      if (document.length == 0) return;
      const xml = document[0].getDataAsString();
      const root = XmlService.parse(xml).getRootElement();
      const n1 = root.getNamespace("w");
      const body = root.getChild("body", n1).getChildren("tbl", n1)
      const obj = body.map((e, i) => {
        const temp = {};
        const tblPr = e.getChild("tblPr", n1);
        if (tblPr) {
          const tblW = tblPr.getChild("tblW", n1);
          if (tblW) {
            const w = tblW.getAttribute("w", n1);
            if (w) {
              temp.tableWidth = Number(w.getValue()) / 20;  // Here, the unit of "dxa" is converted to "pt"
            }
          }
        }
        return temp;
      });
      if (obj.length > 0) console.log(obj);
    
      // DriveApp.getFiles()  // This is used for including the scope of `https://www.googleapis.com/auth/drive.readonly`.
    }