简体   繁体   English

Apps 脚本在文档中制作表格 - 无法设置列宽(子索引错误)

[英]Apps Script making table in doc - Can't set column widths (Child index Error)

I have build some quote to generate a sales quote, I have successfully added all the items into the quote using a Google Doc template.我已经构建了一些报价来生成销售报价,我已经使用 Google 文档模板成功地将所有项目添加到报价中。 One issue I am getting is when trying to style the table, I keep getting this error when I have the setColumnWidth added in我遇到的一个问题是在尝试设置表格样式时,当我添加了 setColumnWidth 时,我不断收到此错误

在此处输入图像描述

The table is being populated from a mult-dymentional array, works if the setColumnWidth isn't there该表是从多维数组填充的,如果 setColumnWidth 不存在则有效

在此处输入图像描述

var developerTableArray = quoteItem.developerItems;

var devloperTableSection = body.findText("{devloperTable}");
var devloperElement = devloperTableSection.getElement();

var childIndexDevloper = body.getChildIndex(devloperElement.getParent());

body.getChild(childIndexDevloper).asText().setText('');
body.insertTable(childIndexDevloper,developerTableArray).setColumnWidth(1, 60);

This is the code I am using to add the table in, am I just doing something wrong?这是我用来添加表格的代码,我是不是做错了什么? Any help would be so appreciated!!任何帮助将不胜感激!

As per the docs setColumnWidth takes two parameters: the first is columnIndex - the index of the column. 根据文档setColumnWidth有两个参数:第一个是columnIndex - 列的索引。

Index 1 refers to the second column of the table.索引1指的是表的第二列。 It seems however that your table contains less than 2 columns which is throwing the error.但是,您的表似乎包含少于 2 列,这是引发错误的原因。

I recommend you to have a look at the contents of developerTableArray to see where the problem lies.我建议你看看developerTableArray的内容,看看问题出在哪里。

Here is an example of how to set the column width for a new table inserted in place of some place holder.下面是一个示例,说明如何为插入的新表格设置列宽以代替某些占位符。

function addTable() {
  try {
    let data = [[1,2,3],[4,5,6],[7,8,9]];
    let doc = DocumentApp.getActiveDocument();
    let body = doc.getBody();
    let element = body.findText("{devloperTable}").getElement()
    let parent = element.getParent();
    let index = body.getChildIndex(parent);
    let table = body.insertTable(index,data);
    table.setColumnWidth(1,60);
    element.removeFromParent();
  }
  catch(err) {
    console.log(err)
  }
}

在此处输入图像描述

References参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM