Google Apps 脚本无法识别我文档中的表格

Google Apps Script does not recognize tables in my document

首先感谢大家在这个项目之前帮助解决问题,这对解决以往的问题有很大的价值。 我正在使用 google 应用程序脚本来格式化 table 内的文档标题。 正常的文字是这样的:

脚本运行后如下所示:

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

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

验证脚本如下:

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);
}

这个答案怎么样?

问题和解决方法:

我可以使用您共享的示例 Google 文档确认相同的问题。在这种情况下,updateTables() 完成后,似乎 getTables() 不会 return 文档正文中的表格。我认为这可能是一个错误。我认为这可能也会影响到当前的问题。所以,为了避免这个问题,我想建议使用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);
}

参考文献: