有没有一种方法可以使用Google Apps脚本清除表中的docs表格式?

时间:2019-09-09 16:18:49

标签: google-apps-script formatting documentation

我正在使用Google App脚本自动生成Google文档,当我插入表格时,它的格式会导致单元格过高。

我已经尝试设置最小高度,但是不能超出某个点并且它仍然过高。 ui中提供了该功能以清除所有表格式,这将提供所需的结果。有没有办法使用Google应用脚本来做到这一点?我似乎找不到解决方案。

在相关情况下格式化文档的其他部分:

  var headerAttributes = {}; 

       headerAttributes[DocumentApp.Attribute.FOREGROUND_COLOR] = '#BFBFBF';
       headerAttributes[DocumentApp.Attribute.FONT_SIZE] = 25;
       headerAttributes[DocumentApp.Attribute.BOLD] = true;
       headerAttributes[DocumentApp.Attribute.UNDERLINE] = true;
       headerAttributes[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = 
            DocumentApp.HorizontalAlignment.CENTER;

  var dateAttributes = {};

       dateAttributes[DocumentApp.Attribute.FOREGROUND_COLOR] = '#FF0000';
       dateAttributes[DocumentApp.Attribute.FONT_SIZE] = 18;
       dateAttributes[DocumentApp.Attribute.BOLD] = false;
       dateAttributes[DocumentApp.Attribute.UNDERLINE] = false;
       dateAttributes[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = 
            DocumentApp.HorizontalAlignment.LEFT;

  var bodyAttributes = {};

       bodyAttributes[DocumentApp.Attribute.FOREGROUND_COLOR] = '#000000';

表创建:

 var cells = [['XXX','Zip','XXX','Bootstrap'],
              ['','','','',],['','','','',],['','','','',],['','','','',],['','','','',],['','','','',],
              ['','','','',],['','','','',],['','','','',],['','','','',],['','','','',],['','','','',]];

  var DLTable = body.appendTable(cells);  

  var headRow = DLTable.getRow(0);

  headRow.setMinimumHeight(10);

  for(var i = 0 ;i < 4; i++){

       headRow.getCell(i).setBackgroundColor('#BDBDBD');

  }

1 个答案:

答案 0 :(得分:0)

通过使用setMinimumHeight()函数(参数为0)可以达到表中行的最小高度:

  for (var i=0; i<cells.length; i++) {
    var row = DLTable.getRow(i);
    row.setMinimumHeight(0);   
  }

在UI中,您可以使用“清除格式”按钮,该按钮将清除所选内容的文本格式,其中的更改是行间距从默认的1.15变为单行(1)。这将使细胞不那么高。您可以使用Google文档高级服务[1]从Apps Scrip进行更改。您需要发出一个updateParagraphStyle请求[2] [3],并将paralineStyle主体内的属性“ lineSpacing”设置为100(单个)。这是您需要添加的代码:

  var requests = [
    {
      'updateParagraphStyle': {
        'range': {
          'startIndex': 1,
          'endIndex':  139
        },
        'paragraphStyle': {
          'lineSpacing': 100,
        },
        'fields': 'lineSpacing'
      }
    }];
  Docs.Documents.batchUpdate({'requests': requests}, 'DOC-ID');

您将需要更改/查找表格的正确范围。您可以查看有关文档结构的指南[4]。

[1] https://developers.google.com/apps-script/advanced/docs

[2] https://developers.google.com/docs/api/how-tos/format-text

[3] https://developers.google.com/docs/api/reference/rest/v1/documents/request#UpdateParagraphStyleRequest

[4] https://developers.google.com/docs/api/concepts/structure