如何使用Google Apps脚本在Google文档页面中将表格居中 [英] How do I center a table in a google document page using Google Apps Script

查看:110
本文介绍了如何使用Google Apps脚本在Google文档页面中将表格居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Google Apps脚本功能在Google文档中插入了一个表格

I have inserted a table in a google docs using the Google Apps Script function

var grg = body.appendTable(griglia);

我能够格式化表格中的文本(字体大小,粗细,对齐方式),并且可以格式化单个单元格(背景,前景,边框).

I am able to format the text in the table (font size, weight, alignment) and I can format individual cells (background, foreground, borders).

不过,表格是左对齐的,我希望它(水平)在页面中居中.在google docs用户界面中,可以通过表格>表格属性"菜单完成此操作,该菜单会生成一个带有表格对齐"下拉菜单的对话框.

However the table is aligned left, and I would like it to be (horizontally) centered in the page. From the google docs user interface this can be done from the menu "Table>Table properties", which generates a dialog box with a dropdown menu for "Table alignment".

我找不到从脚本中获取相同内容的方法.我尝试过

I cannot find a way to obtain the same from the script. I tried

var grg = body.appendTable(griglia).setAlignment(DocumentApp.HorizontalAlignment.CENTER);

但是出现错误,因为未为表定义setAlignment函数(这适用于段落).

but I get an error because the setAlignment function is not defined for a table (this works for paragraphs).

我也尝试通过定义样式

  var gTxt={};
  gTxt[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER;
  grg.setAttribute(gTxt);

但是这似乎对表没有任何影响.实际上,我也尝试过使用其他属性,只是为了了解事物的工作原理.

but this does not seem to produce any effect on the table. Actually I also tried with a different property, just to try to understand how things work.

使用

gTxt[DocumentApp.Attribute.FOREGROUND_COLOR] = '#ff0000';
grg.setAttribute(gTxt);

表中的所有文本及其后面的所有内容均变为红色.因此,似乎setAttributes并不局限于"表.我很困惑,因为在文档中 "setAttributes"列在表格"下(尽管在示例中是在段落中使用的).

all the text in the table, and whatever follows it, becomes red. So it seems that the setAttributes is not "limited" to the table. I am confused, because in the documentation "setAttributes" is listed under "Table" (although in the example it is used on a paragraph).

我进行了广泛的研究,但只发现了一个元帖子所了解,我的问题不应被标记为重复,因为较旧的问题并未得到真正回答.

I did extensive research, but found only one similar question (in fact basically identical) dating back two years. As far as I understand from this meta post, my question should not be marked as a duplicate, since the older question was not really answered.

只有一个答案,但是它的作者误解了这个问题.这里的重点是表格居中,而不是表格单元格中的文本(我可以做到这一点).

There is only an answer, but its author misunderstood the question. The point here is centering the table, and not the text in its cells (which I can do no problem).

我试图对旧问题进行投票或评论,但由于我只提出几点意见而无法这样做.

I tried to upvote or comment the old question, but was not able because I have only a few points.

感谢您的见解.

推荐答案

目前无法通过Apps脚本将表居中.

It is not possible to center table via Apps Script at the moment.

在Google跟踪器中创建了功能请求,您可以对其加注星标": https://issuetracker.google.com/issues/36765133

There's feature request created in Google tracker, you can "star" it: https://issuetracker.google.com/issues/36765133

这篇关于如何使用Google Apps脚本在Google文档页面中将表格居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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