使用Google Slides Apps脚本设置表格的宽度和高度 [英] Setting width and height of tables with Google Slides Apps Script

查看:410
本文介绍了使用Google Slides Apps脚本设置表格的宽度和高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google Apps脚本来操作幻灯片文件.我想在幻灯片上创建表格,并设置行/列,特定单元格或表格整体的高度/宽度.除了此页面之外,该文档对此均保持沉默.似乎不太有前途.

I'm using Google Apps Script to manipulate a Slides file. I want to create a table on a slide and set the height/width of a row/column, of a particular cell, or of the table overall. The documentation is mysteriously silent on this, except for this page which doesn't seem very promising.

任何人都知道解决此问题的方法,还是我必须求助于Slides API?

Anybody know a workaround for this, or do I have to resort to the Slides API?

推荐答案

  • 您要使用Google Apps脚本修改Google幻灯片上表格的宽度和高度.
  • 您要修改特定单元格的宽度和高度.
  • 我能像上面那样理解.

    创建新表格时,可以使用幻灯片服务"设置表格的高度和宽度.但是在当前阶段,尚无法使用Slides Service修改已创建表格的高度和宽度以及特定的单元格.我认为这可能会在将来的更新中实现.

    You can set the height and width of a table using Slides Service, when new table is created. But in the current stage, the height and width of created table and the the specific cell cannot be modified using using Slides Service, yet. I think that this might be achieved in the future update.

    作为当前的解决方法,当使用Slides API时,可以实现此目的.

    As a current workaround, when Slides API is used, this can be achieved.

    在此示例脚本中,使用了幻灯片服务.在幻灯片的第一页上,分别以宽度和高度分别为300点和100点创建新表格.

    In this sample script, Slides Service is used. New table is created with 300 point and 100 point in the width and height on the 1st page of Slides, respectively.

    var slides = SlidesApp.getActivePresentation();
    var slide = slides.getSlides()[0];
    var table = slide.insertTable(3, 3, 0, 0, 300, 100);
    

    示例脚本2:

    在此示例脚本中,使用了Slides API.将第一页上表格"B2"的单元格的宽度和高度分别修改为300点和100点.

    Sample script 2:

    In this sample script, Slides API is used. The the width and height of the cell "B2" of table on the 1st page are modified to 300 point and 100 point, respectively.

    var slides = SlidesApp.getActivePresentation();
    var slide = slides.getSlides()[0];
    var objectId = slide.getTables()[0].getObjectId();
    SlidesApp.getActivePresentation().saveAndClose();
    var resource = {requests: [
      {updateTableColumnProperties: {tableColumnProperties: 
        {columnWidth: {magnitude: 300, unit: "PT"}},
        columnIndices: [1],
        objectId: objectId,
        fields: "columnWidth"
      }},
      {updateTableRowProperties: {tableRowProperties: 
        {minRowHeight: {magnitude: 100, unit: "PT"}},
        rowIndices: [1],
        objectId: objectId,
        fields: "minRowHeight"
      }}
    ]};
    Slides.Presentations.batchUpdate(resource, slides.getId());
    

    示例脚本3:

    在此示例脚本中,仅使用Slides API,可以获得示例脚本1"和示例脚本2"的结果.

    Sample script 3:

    In this sample script, using only Slides API, the result of "Sample script 1" and "Sample script 2" can be obtained.

    var slides = SlidesApp.getActivePresentation();
    var slide = slides.getSlides()[0];
    var objectId = "sampleTable" + (new Date().getTime());
    var resource = {requests: [
      {createTable: {
        rows: 3,
        columns: 3,
        elementProperties: {pageObjectId: slide.getObjectId(), size: {width: {magnitude: 300, unit: "PT"}, height: {magnitude: 100, unit: "PT"}}},
        objectId: objectId
      }},
      {updateTableColumnProperties: {
        tableColumnProperties: {columnWidth: {magnitude: 300, unit: "PT"}},
        columnIndices: [1],
        objectId: objectId,
        fields: "columnWidth"
      }},
      {updateTableRowProperties: {
        tableRowProperties: {minRowHeight: {magnitude: 100, unit: "PT"}},
        rowIndices: [1],
        objectId: objectId,
        fields: "minRowHeight"
      }}
    ]};
    Slides.Presentations.batchUpdate(resource, slides.getId());
    

    注意:

    • 使用Slides API时,请在高级Google上启用它服务.
      • 从2019年4月8日开始,当创建新的脚本项目(独立脚本类型和容器绑定脚本类型)并在Advanced Google Services启用了API时,必须能够使用这些API.因为保存脚本项目时会在默认的GCP项目中自动启用API.
      • Note:

        • When you use Slides API, please enable it at Advanced Google Services.
          • From April 8, 2019, when new script project (standalone script type and container-bound script type) is created and the APIs are enabled at Advanced Google Services, the APIs got to be able to be used. Because the APIs are automatically enabled in the default GCP project when the script project is saved.
            • insertTable(numRows, numColumns, left, top, width, height)
            • presentations.batchUpdate
            • Slides Service
            • Advanced Google services

            这篇关于使用Google Slides Apps脚本设置表格的宽度和高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆