【问题标题】:Google Apps Script does not recognize tables in my documentGoogle Apps 脚本无法识别我文档中的表格
【发布时间】:2020-04-23 05:14:56
【问题描述】:

首先,我要感谢您在这个项目之前帮助解决问题,这对解决过去的问题非常有价值。 我正在使用谷歌应用程序脚本来格式化表格中文档的标题。 正常的文字是这样的:

脚本运行后如下所示:

脚本根据需要以正常方式在表格中插入文本,但是除了这种格式之外,我还需要设置标题的样式。示例:标题 1 通常具有 Arial 18 字体,没有粗体,我想将其更改为带粗体的 Roboto 18 字体。 我尝试使用自定义样式的谷歌应用程序,但在脚本处理中,格式在通过这行代码时会丢失。

我已经尝试在更新过程后恢复表格并对其进行格式化,但更新过程后系统无法将表格识别为表格,只有最后一个格式化的标题保持所需格式,如图所示第二张图片。查看我的调试过程的一些打印。 第一个标题被更改并放置在表格中,并应用格式并识别插入的表格: 当脚本到达第二种方法的saveAndClose()点时,前面标题的自定义就消失了: 在该过程结束时,只有最后一个自定义标题保持所需格式。 我已经尝试恢复插入的表以执行第二列文本样式的更新,但脚本无法识别这些表。它只识别一个,事实上,在这个文档中我有 4 个表。

这是一个验证脚本:

function verifiStyle(){
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  var paragrafs = body.getParagraphs();
  
  for(var i = paragrafs.length - 1; i >= 0; i--){
    var attr = paragrafs[i].getAttributes();
    
    for(var at in attr){
      if(at == "HEADING" & attr[at] == "HEADING1"){
        VerifTitle1(i);
      }
      else if(at == "HEADING" & attr[at] == "HEADING2"){
        VerifTitle2(i);
      }
      else if(at == "HEADING" & attr[at] == "HEADING3"){
        VerifTitle3(i);
      }
      else if(at == "HEADING" & attr[at] == "HEADING4"){
        VerifTitle4(i);
      }
      else if(at == "HEADING" & attr[at] == "NORMAL"){
        VerifTextoNormal(i);
      }
    }
  }
  var tables = body.getTables();
}

function VerifTitle1(value){
   var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  
  var texto = body.getChild(value);
  var ttt = texto.getText();
  
  var cells = [
      ['', '']
    ];
  
  var styleCell1 = {};
    styleCell1[DocumentApp.Attribute.FONT_SIZE] = 20;
    styleCell1[DocumentApp.Attribute.VERTICAL_ALIGNMENT] = DocumentApp.VerticalAlignment.CENTER;
    styleCell1[DocumentApp.Attribute.FOREGROUND_COLOR]='#888888';
    styleCell1[DocumentApp.Attribute.FONT_FAMILY]='Roboto';
    
    var styleCell = {};
    styleCell[DocumentApp.Attribute.VERTICAL_ALIGNMENT] = DocumentApp.VerticalAlignment.CENTER;
    styleCell[DocumentApp.Attribute.FONT_FAMILY]='Roboto';
    styleCell[DocumentApp.Attribute.FONT_SIZE] = 18;
    styleCell[DocumentApp.Attribute.FOREGROUND_COLOR]='#000000';
    styleCell[DocumentApp.Attribute.HEIGHT] = 0.5;
  
  body.removeChild(body.getChild(value));
  var table = body.insertTable(value, cells);
  
    table.getRow(0).getCell(1).appendParagraph(ttt).setHeading(DocumentApp.ParagraphHeading.HEADING2);
    table.getRow(0).getCell(1).setAttributes(styleCell);
    table.getRow(0).getCell(0).setWidth(2);
    table.getRow(0).getCell(0).setAttributes(styleCell1);
    table.setBorderColor('#ffffff');
  table.getRow(0).editAsText().setBold(true);
    
    const index = body.getChildIndex(table);
    const documentId = doc.getId();
    doc.saveAndClose();
    const tableStart = Docs.Documents.get(documentId).body.content[index + 1].startIndex;
    const tempStyle = {width: {magnitude :0, unit: "PT"}, dashStyle: "SOLID", color: {color: {rgbColor: {blue: 0}}}};
    const resource = {requests: [
      {updateTableCellStyle: {
        tableStartLocation: {index: tableStart},
        tableCellStyle: {borderTop: tempStyle, borderBottom: tempStyle, borderLeft: tempStyle, borderRight: tempStyle},
        fields: "borderTop,borderBottom,borderLeft,borderRight"
      }},
      {updateTableCellStyle: {
        tableRange: {
          tableCellLocation: {tableStartLocation: {index: tableStart}, rowIndex: 0, columnIndex: 0}, rowSpan: 1, columnSpan: 1},
        tableCellStyle: {
          borderRight: {dashStyle: "SOLID", width: {magnitude: 3, unit: "PT"}, color: {color: {rgbColor: {red: 0.9372549019607843, green: 0.3254901960784314, blue: 0.3137254901960784}}}}
        },
        fields: "borderRight"
      }}
    ]};
    Docs.Documents.batchUpdate(resource, documentId);
  
  table = body.getChild(value).asTable();
}

function VerifTitle2(value){
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  
  var texto = body.getChild(value);
  var ttt = texto.getText();
  
  var cells = [
      ['', '']
    ];
  
  var styleCell1 = {};
    styleCell1[DocumentApp.Attribute.FONT_SIZE] = 18;
    styleCell1[DocumentApp.Attribute.VERTICAL_ALIGNMENT] = DocumentApp.VerticalAlignment.CENTER;
    styleCell1[DocumentApp.Attribute.FOREGROUND_COLOR]='#888888';
    styleCell1[DocumentApp.Attribute.FONT_FAMILY]='Roboto';
    
    var styleCell = {};
    styleCell[DocumentApp.Attribute.VERTICAL_ALIGNMENT] = DocumentApp.VerticalAlignment.CENTER;
    styleCell[DocumentApp.Attribute.FONT_FAMILY]='Roboto';
    styleCell[DocumentApp.Attribute.FONT_SIZE] = 15;
    styleCell[DocumentApp.Attribute.FOREGROUND_COLOR]='#000000';
    styleCell[DocumentApp.Attribute.HEIGHT] = 0.5;
  
  body.removeChild(body.getChild(value));
  var table = body.insertTable(value, cells);
  
    table.getRow(0).getCell(1).appendParagraph(ttt).setHeading(DocumentApp.ParagraphHeading.HEADING2);
    table.getRow(0).getCell(1).setAttributes(styleCell);
    table.getRow(0).getCell(0).setWidth(2);
    table.getRow(0).getCell(0).setAttributes(styleCell1);
    table.setBorderColor('#ffffff');
  table.getRow(0).editAsText().setBold(true);
    
    const index = body.getChildIndex(table);
    const documentId = doc.getId();
    doc.saveAndClose();
    const tableStart = Docs.Documents.get(documentId).body.content[index + 1].startIndex;
    const tempStyle = {width: {magnitude :0, unit: "PT"}, dashStyle: "SOLID", color: {color: {rgbColor: {blue: 0}}}};
    const resource = {requests: [
      {updateTableCellStyle: {
        tableStartLocation: {index: tableStart},
        tableCellStyle: {borderTop: tempStyle, borderBottom: tempStyle, borderLeft: tempStyle, borderRight: tempStyle},
        fields: "borderTop,borderBottom,borderLeft,borderRight"
      }},
      {updateTableCellStyle: {
        tableRange: {
          tableCellLocation: {tableStartLocation: {index: tableStart}, rowIndex: 0, columnIndex: 0}, rowSpan: 1, columnSpan: 1},
        tableCellStyle: {
          borderRight: {dashStyle: "SOLID", width: {magnitude: 3, unit: "PT"}, color: {color: {rgbColor: {red: 0.9372549019607843, green: 0.3254901960784314, blue: 0.3137254901960784}}}}
        },
        fields: "borderRight"
      }}
    ]};
    Docs.Documents.batchUpdate(resource, documentId);
}

function VerifTitle3(value){
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  
  var texto = body.getChild(value);
  var ttt = texto.getText();
  
  var cells = [
      ['', '']
    ];
  
  var styleCell1 = {};
    styleCell1[DocumentApp.Attribute.FONT_SIZE] = 16;
    styleCell1[DocumentApp.Attribute.VERTICAL_ALIGNMENT] = DocumentApp.VerticalAlignment.CENTER;
    styleCell1[DocumentApp.Attribute.FOREGROUND_COLOR]='#888888';
    styleCell1[DocumentApp.Attribute.FONT_FAMILY]='Roboto';
    
    var styleCell = {};
    styleCell[DocumentApp.Attribute.VERTICAL_ALIGNMENT] = DocumentApp.VerticalAlignment.CENTER;
    styleCell[DocumentApp.Attribute.FONT_FAMILY]='Roboto';
    styleCell[DocumentApp.Attribute.FONT_SIZE] = 14;
    styleCell[DocumentApp.Attribute.FOREGROUND_COLOR]='#000000';
    styleCell[DocumentApp.Attribute.HEIGHT] = 0.5;
  styleCell[DocumentApp.Attribute.BOLD] = true;
  
  body.removeChild(body.getChild(value));
  var table = body.insertTable(value, cells);
  
    table.getRow(0).getCell(1).appendParagraph(ttt).setHeading(DocumentApp.ParagraphHeading.HEADING3);
    table.getRow(0).getCell(1).setAttributes(styleCell);
    table.getRow(0).getCell(0).setWidth(2);
    table.getRow(0).getCell(0).setAttributes(styleCell1);
    table.setBorderColor('#ffffff');
  table.getRow(0).editAsText().setBold(true);
    
    const index = body.getChildIndex(table);
    const documentId = doc.getId();
    doc.saveAndClose();
    const tableStart = Docs.Documents.get(documentId).body.content[index + 1].startIndex;
    const tempStyle = {width: {magnitude :0, unit: "PT"}, dashStyle: "SOLID", color: {color: {rgbColor: {blue: 0}}}};
    const resource = {requests: [
      {updateTableCellStyle: {
        tableStartLocation: {index: tableStart},
        tableCellStyle: {borderTop: tempStyle, borderBottom: tempStyle, borderLeft: tempStyle, borderRight: tempStyle},
        fields: "borderTop,borderBottom,borderLeft,borderRight"
      }},
      {updateTableCellStyle: {
        tableRange: {
          tableCellLocation: {tableStartLocation: {index: tableStart}, rowIndex: 0, columnIndex: 0}, rowSpan: 1, columnSpan: 1},
        tableCellStyle: {
          borderRight: {dashStyle: "SOLID", width: {magnitude: 3, unit: "PT"}, color: {color: {rgbColor: {red: 0.9372549019607843, green: 0.3254901960784314, blue: 0.3137254901960784}}}}
        },
        fields: "borderRight"
      }}
    ]};
    Docs.Documents.batchUpdate(resource, documentId);
}

function VerifTitle4(value){
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  
  var texto = body.getChild(value);
  var ttt = texto.getText();
  
  var cells = [
      ['', '']
    ];
  
  var styleCell1 = {};
    styleCell1[DocumentApp.Attribute.FONT_SIZE] = 14;
    styleCell1[DocumentApp.Attribute.VERTICAL_ALIGNMENT] = DocumentApp.VerticalAlignment.CENTER;
    styleCell1[DocumentApp.Attribute.FOREGROUND_COLOR]='#888888';
    styleCell1[DocumentApp.Attribute.FONT_FAMILY]='Roboto';
    
    var styleCell = {};
    styleCell[DocumentApp.Attribute.VERTICAL_ALIGNMENT] = DocumentApp.VerticalAlignment.CENTER;
    styleCell[DocumentApp.Attribute.FONT_FAMILY]='Roboto';
    styleCell[DocumentApp.Attribute.FONT_SIZE] = 12;
    styleCell[DocumentApp.Attribute.FOREGROUND_COLOR]='#000000';
    styleCell[DocumentApp.Attribute.HEIGHT] = 0.5;
  
  body.removeChild(body.getChild(value));
  var table = body.insertTable(value, cells);
  
  var tables = body.getTables();
  
    table.getRow(0).getCell(1).appendParagraph(ttt).setHeading(DocumentApp.ParagraphHeading.HEADING2);
    table.getRow(0).getCell(1).setAttributes(styleCell);
    table.getRow(0).getCell(0).setWidth(2);
    table.getRow(0).getCell(0).setAttributes(styleCell1);
    table.setBorderColor('#ffffff');
  table.getRow(0).editAsText().setBold(true);
    
    const index = body.getChildIndex(table);
    const documentId = doc.getId();
    doc.saveAndClose();
    const tableStart = Docs.Documents.get(documentId).body.content[index + 1].startIndex;
    const tempStyle = {width: {magnitude :0, unit: "PT"}, dashStyle: "SOLID", color: {color: {rgbColor: {blue: 0}}}};
    const resource = {requests: [
      {updateTableCellStyle: {
        tableStartLocation: {index: tableStart},
        tableCellStyle: {borderTop: tempStyle, borderBottom: tempStyle, borderLeft: tempStyle, borderRight: tempStyle},
        fields: "borderTop,borderBottom,borderLeft,borderRight"
      }},
      {updateTableCellStyle: {
        tableRange: {
          tableCellLocation: {tableStartLocation: {index: tableStart}, rowIndex: 0, columnIndex: 0}, rowSpan: 1, columnSpan: 1},
        tableCellStyle: {
          borderRight: {dashStyle: "SOLID", width: {magnitude: 3, unit: "PT"}, color: {color: {rgbColor: {red: 0.9372549019607843, green: 0.3254901960784314, blue: 0.3137254901960784}}}}
        },
        fields: "borderRight"
      }}
    ]};
    Docs.Documents.batchUpdate(resource, documentId);
  var tables1 = body.getTables();
}

function VerifTextoNormal(value){
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  
  var para = body.getParagraphs();
  
  var styleCell = {};
    styleCell[DocumentApp.Attribute.HEADING] = DocumentApp.ParagraphHeading.NORMAL;
    styleCell[DocumentApp.Attribute.VERTICAL_ALIGNMENT] = DocumentApp.VerticalAlignment.CENTER;
    styleCell[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.JUSTIFY;
    styleCell[DocumentApp.Attribute.FONT_FAMILY]='Arial';
    styleCell[DocumentApp.Attribute.FONT_SIZE] = 12;
    styleCell[DocumentApp.Attribute.FOREGROUND_COLOR]='#000000';
    styleCell[DocumentApp.Attribute.INDENT_FIRST_LINE] = 15;
  
  para[value].setAttributes(styleCell);
}

【问题讨论】:

  • 您是否尝试过re-opening 在 saveClose 后再次获取 Doc Id 的 Doc?真的有必要做saveAndClose吗?
  • SaveAndClose 真的很有必要,因为没有它,更新就无法工作。这个重新打开并检索id并进行更改的建议,我也尝试过。关键是,正如我所说,脚本无法将我的表格识别为表格。
  • 很遗憾,我仍然无法复制您的情况。这是因为我的技术不好。我对此深表歉意。为了复制您的问题并思考解决方案,您能否提供用于复制您的问题的示例文档?
  • 问候@Tanaike。我的文档的链接是这个 (docs.google.com/document/d/…)。感谢您的帮助。
  • 感谢您的回复和补充信息。当我测试它时,我可以确认同样的问题。所以我提出了一个解决方法作为答案。你能确认一下吗?如果这不是您想要的方向,我很抱歉。

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


【解决方案1】:

这个答案怎么样?

问题和解决方法:

我可以使用您共享的示例 Google 文档来确认同样的问题。在这种情况下,updateTables() 完成后,getTables() 似乎没有返回 Document 正文中的表格。我认为这可能是一个错误。我认为这也可能会影响当前的问题。所以,为了避免这个问题,我建议使用Docs API。

在您的脚本中,当verifiStyle() 运行时,它会更新verifiStyle() 最后一行的表格。这是一种解决方法。

修改脚本:

当你的脚本被修改时,请进行如下修改。

从:

这是函数verifiStyle()最后一行的脚本。

  var tables = body.getTables();
}
到:
  updateTables();  // Modified
}

// Added the below function.
function updateTables() {
  const docId = DocumentApp.getActiveDocument().getId();
  const contents = Docs.Documents.get(docId).body.content;
  const reqs = contents.reduce((ar, e) => {
    if ("table" in e) {
      const t = e.table.tableRows[0].tableCells;
      const obj = [
        {updateTextStyle: {
          range: {startIndex: t[0].startIndex, endIndex: t[0].endIndex},
          textStyle: {bold: true, fontSize: {magnitude: 20, unit: "PT"}, weightedFontFamily: {fontFamily: "Roboto"}},fields: "bold,fontSize,weightedFontFamily"}
        },
        {updateTextStyle: {
          range: {startIndex: t[1].startIndex, endIndex: t[1].endIndex},
          textStyle: {bold: true, fontSize: {magnitude: 18, unit: "PT"}, weightedFontFamily: {fontFamily: "Roboto"}}, fields: "bold,fontSize,weightedFontFamily"}
        }
      ];
      ar = [...ar, obj];
    }
    return ar;
  }, []);
  Docs.Documents.batchUpdate({requests: reqs}, docId);
}

参考资料:

【讨论】:

  • 在对您提供给我的原始代码进行了一些调整之后,我的脚本终于可以完全满足我的需要了。谢谢!
  • @Leonardo Silva 感谢您的回复和测试。我很高兴你的问题得到了解决。也谢谢你。
猜你喜欢
  • 2021-09-07
  • 1970-01-01
  • 1970-01-01
  • 2021-10-22
  • 1970-01-01
  • 2020-04-07
  • 1970-01-01
  • 2020-04-29
  • 2015-06-25
相关资源
最近更新 更多