Google Apps 文档脚本中的错误 运行 脚本

Error running script in Google Apps Script for Documents

我一直在尝试使用 Google Apps Script for Documents 创建一个脚本。直到那时解决方案 运行 完美,但自昨晚以来,当 运行 我的脚本作为 follows:Exception 时,我收到一条错误消息: The Documents service failed使用代码 1tk-eeaBSSJ9Im2b5Y-e1jL8t18rKFXhp356udoyx8wA 访问文档。 (第 124 行,文件 "FindAttributes")。在我反转方法调用后,错误更改为: GoogleJsonResponseException: API call to docs.documents.batchUpdate failed with the error Invalid requests [0] 。 updateTableCellStyle:提供的 table 起始位置无效。 (第 83 行,文件 "FindAttributes")。 我在互联网上搜索了有关此类错误的任何参考资料,但我的搜索一无所获。 我的代码检查文本中的标题并将其转换为特定的自定义格式。令我感兴趣的是,它只发生在涉及 HEADING1 样式时,这是我的脚本中第一个访问的样式。当我从 HEADING1 覆盖此方法时,其他方法可以正常工作。有谁知道可能发生了什么?我发现它 st运行ge 因为代码完全相同,除了样式格式部分。

我的代码:

function verifiStyle(){
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  var paragrafs = body.getParagraphs();
  
  for(var i = 0; i < paragrafs.length; 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);
      }
    }
  }
}

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.HEADING] = DocumentApp.ParagraphHeading.HEADING1;
    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] = 10
  
  body.removeChild(body.getChild(value));
  var table = body.insertTable(value, cells);
  
    table.getRow(0).getCell(1).appendParagraph(ttt).setHeading(DocumentApp.ParagraphHeading.HEADING1);
    table.getRow(0).getCell(1).setAttributes(styleCell);
    table.getRow(0).getCell(0).setWidth(40);
    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 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.HEADING] = DocumentApp.ParagraphHeading.HEADING2;
    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] = 10
  
  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(40);
    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.HEADING] = DocumentApp.ParagraphHeading.HEADING3;
    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] = 10
  
  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(40);
    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.HEADING] = DocumentApp.ParagraphHeading.HEADING4;
    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] = 10
  
  body.removeChild(body.getChild(value));
  var table = body.insertTable(value, cells);
  
    table.getRow(0).getCell(1).appendParagraph(ttt).setHeading(DocumentApp.ParagraphHeading.HEADING4);
    table.getRow(0).getCell(1).setAttributes(styleCell);
    table.getRow(0).getCell(0).setWidth(40);
    table.getRow(0).getCell(0).setAttributes(styleCell1);
    table.setBorderColor('#ffffff');
  table.getRow(0).editAsText().setBold(true);
  table.getRow(0).setMinimumHeight(0.2);
    
    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 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);
}

这是启用 VerifiTitle1() 时我的脚本的结果。

这是禁用VerifiTitle1()时的结果。

也就是。用这种方法无法访问。一切顺利。

修改点:

我认为您出现问题的原因是使用了 for(var i = 0; i < paragrafs.length; i++)removeChild()。在这种情况下,子项将从文档正文的顶部移除。这样,文档主体的子项就被更改了。那么比如下面的修改呢?

修改后的脚本:

请按如下方式修改verifiStyle()中的脚本。

从:
for(var i = 0; i < paragrafs.length; i++){
到:
for(var i = paragrafs.length - 1; i >= 0; i--){