Google脚本在Docs表的同一表格行中显示不同的对象

时间:2016-02-17 22:21:48

标签: google-apps-script google-docs google-docs-api

我试图通过谷歌应用程序脚本将三个段落(从谷歌电子表格加载)放入文档。

一个应该在它自己的行上,另外两个应该在同一行但具有不同的对齐方式。

产品图。 1 - 我想要什么

enter image description here

问题是,谷歌只允许在表格单元格中附加段落。并且段落包含新行,因此第二个文本在新行上。

产品图。 2 - 我得到了什么

enter image description here

我尝试添加段落然后附加文字。 但我不知道如何设置右对齐标签并在第一个文本后插入标签。

是否可以仅使用谷歌脚本设置标签?

我欢迎任何帮助或建议如何创建Pic中显示的文本。 1.比你任何帮助。

1 个答案:

答案 0 :(得分:1)

实际上an issue (3211)无法完全按照您的要求执行操作:merge()方法导致文档崩溃。

所以下面的代码会产生如下代码:

enter image description here

因为我们无法合并脚本第一行中的2个单元格。

手动操作效果很好:

enter image description here

代码:

function myFunction() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  body.clear();
  var table = body.appendTable([['',''],['','']]);
  table.setBorderColor('#ffffff');
  table.getCell(0,0).appendParagraph('Some important text important text important text important text ');
  //table.getCell(0,1).merge();// this is the issue...it crashes the doc
  table.getCell(1,0).appendParagraph('left text').setAlignment(DocumentApp.HorizontalAlignment.LEFT);                         
  table.getCell(1,1).appendParagraph('right text').setAlignment(DocumentApp.HorizontalAlignment.RIGHT);                             
  doc.saveAndClose()
}