【问题标题】:Error running script in Google Apps Script for Documents在 Google Apps Script for Documents 中运行脚本时出错
【发布时间】:2020-04-22 01:15:03
【问题描述】:

我一直在尝试使用 Google Apps Script for Documents 创建一个脚本。在那之前,解决方案运行完美,但从昨晚开始,我在运行脚本时收到如下错误消息:异常:文档服务无法访问代码为 1tk-eeaBSSJ9Im2b5Y-e1jL8t18rKFXhp356udoyx8wA 的文档。 (第 124 行,文件“FindAttributes”)。在我反转方法调用后,错误更改为:GoogleJsonResponseException: API call to docs.documents.batchUpdate failed with the error Invalid requests [0] .updateTableCellStyle: The provided table start location is invalid。 (第 83 行,文件“FindAttributes”)。 我在互联网上搜索了有关此类错误的任何参考,但我的搜索结果一无所获。 我的代码检查文本中的标题并将其转换为某种自定义格式。让我感兴趣的是,它仅在涉及 HEADING1 样式时出现,这是我的脚本中第一个访问的样式。当我从 HEADING1 覆盖此方法时,其他方法可以正常工作。有谁知道可能发生了什么?我觉得很奇怪,因为代码完全一样,除了样式格式部分。

我的代码:

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 () 时的结果。

就是这样。使用此方法无法访问。一切顺利。

【问题讨论】:

  • 请将错误翻译成英文。
  • 您是否在 GCP 中启用了 Google Docs API?
  • 是的,我在 GCP 中激活了 Google Docs API。我觉得奇怪的是其他功能正常工作。只有第一个“VerifiTitle1”有问题,奇怪的是代码都差不多,除了一些样式设置。

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


【解决方案1】:

修改点:

我认为您的问题的原因是使用for(var i = 0; i &lt; paragrafs.length; i++)removeChild()。在这种情况下,孩子会从文档正文的顶部移除。这样,Document body 的子节点就发生了变化。那么比如下面的修改呢?

修改脚本:

请将verifiStyle()中的脚本修改如下。

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

【讨论】:

  • 总的来说,您的解决方案对我来说效果很好。我必须做的唯一观察是字体系列、字体大小和前景色格式仅适用于列表的最后一个元素。其他元素仅采用标题类型和边框格式。你能告诉我发生了什么事吗?
  • @Leonardo Silva 感谢您的回复。我很高兴你的问题得到了解决。关于您的附加问题,我从您的回复中无法理解。这是因为我的技术不好。我对此深表歉意。为了正确理解它,您可以通过包含更多信息将其作为新问题发布吗?借此,我想确认一下。
猜你喜欢
  • 2016-08-06
  • 1970-01-01
  • 1970-01-01
  • 2020-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多