4

我正在尝试从模板文件在 Google 文档中生成报告。当它复制文档时,它会将所有格式重置为用户的默认格式,而不是原始文档中的格式。我尝试了以下方法来尝试在文档、tableRow 和 tableCell 级别上设置格式,尽管创建报告时行距为 1.5 并且段落后有空格

var style = {};
style[DocumentApp.Attribute.SPACING_AFTER] =0;
style[DocumentApp.Attribute.SPACING_BEFORE] =0;
style[DocumentApp.Attribute.LINE_SPACING]=1;   

var newrow= tables[2].insertTableRow(n).setBold(false).setAttributes(style);
   if(j==0){
     newrow.insertTableCell(0,reportDate).setPaddingBottom(0).setPaddingTop(0).setAttributes(style);
   }
   else{
     newrow.insertTableCell(0,'').setPaddingBottom(0).setPaddingTop(0).setAttributes(style);
   }
   newrow.insertTableCell(0,values1[rowId1][1]+' '+values1[rowId1][2]).setPaddingBottom(0).setPaddingTop(0).setAttributes(style);
   newrow.insertTableCell(0,'').setPaddingBottom(0).setPaddingTop(0).setAttributes(style); 
   doc.editAsText().setAttributes(style);

关于如何让报告遵循这些属性的任何建议?

4

3 回答 3

2

我相信 SPACING_AFTER、SPACING_BEFORE 和 LINE_SPACING 不是与 TABLE_CELL 对象关联的属性。您必须引用子 PARAGRAPH 才能设置这些。

var style = {};
  style[DocumentApp.Attribute.SPACING_AFTER] =0;
  style[DocumentApp.Attribute.SPACING_BEFORE] =0;
  style[DocumentApp.Attribute.LINE_SPACING]=1;   

  var newrow = tables[2]
    .insertTableRow(n)
    .setBold(false);

  if (j == 0) {
    newrow.insertTableCell(0,reportDate)
      .setPaddingBottom(0)
      .setPaddingTop(0);
  }
  else {
    newrow.insertTableCell(0,'').setPaddingBottom(0).setPaddingTop(0);
  }
  newrow.insertTableCell(0,values1[rowId1][1]+' '+values1[rowId1][2])
    .setPaddingBottom(0)
    .setPaddingTop(0);
  newrow.insertTableCell(0,'')
    .setPaddingBottom(0)
    .setPaddingTop(0); 
  var newrowTableCell = newrow.getChild(0);
  var newrowParagraph = newrowTableCell.getChild(0).setAttributes(style);
于 2014-03-20T03:22:52.343 回答
1

由于设置的属性没有设置属性并且您不能将任何元素转换为段落,我通过获取所有段落并通过在末尾添加手动设置它们来解决这个问题

var p=doc.getParagraphs();
  for(i=0;i<p.length; i++){
    p[i].setLineSpacing(1).setSpacingAfter(0);
于 2012-11-29T18:30:25.987 回答
0

用户是否需要能够编辑报告,或者只是查看它?为什么不生成pdf?每次我完成它,它都会保存格式。

var file = DocsList.getFileById('1DIfn_wVpXSI4hU5zG43Fvp2ZdpUP_KqgtgFRT9NWJ7E');
var newFile = DocsList.copy(file, ename+'-'+reportDate+'Monthly Report');
var report=DocsList.createFile(newFile.getAs("application/pdf")).rename(newFile.getName() + ".pdf");
var a = report.getID();
var doc = DocumentApp.openById(a);
于 2012-11-19T19:13:45.027 回答