简体   繁体   English

在 Google Apps Script for Documents 中运行脚本时出错

[英]Error running script in Google Apps Script for Documents

I have been trying to create a script with Google Apps Script for Documents.我一直在尝试使用 Google Apps Script for Documents 创建一个脚本。 Until then the solution ran perfectly, but since last night I have received an error message when running my script as follows:Exception: The Documents service failed to access the document with the code 1tk-eeaBSSJ9Im2b5Y-e1jL8t18rKFXhp356udoyx8wA.在那之前,解决方案运行完美,但从昨晚开始,我在运行脚本时收到如下错误消息:异常:文档服务无法访问代码为 1tk-eeaBSSJ9Im2b5Y-e1jL8t18rKFXhp356udoyx8wA 的文档。 (line 124, file "FindAttributes") . (第 124 行,文件“FindAttributes”) After I inverted the method call the error changed to: GoogleJsonResponseException: API call to docs.documents.batchUpdate failed with the error Invalid requests [0].updateTableCellStyle: The provided table start location is invalid.在我反转方法调用后,错误更改为: GoogleJsonResponseException: API call to docs.documents.batchUpdate failed with the error Invalid requests [0].updateTableCellStyle: The provided table start location is invalid。 (line 83, file "FindAttributes") . (第 83 行,文件“FindAttributes”) I searched the internet for any reference to this type of error, but my searches resulted in nothing.我在互联网上搜索了有关此类错误的任何参考,但我的搜索结果一无所获。 My code checks for titles in a text and converts it to a certain custom format.我的代码检查文本中的标题并将其转换为某种自定义格式。 What intrigues me is that it only occurs when it comes to the HEADING1 style, which is the first to be accessed in my script.令我感兴趣的是,它仅在涉及 HEADING1 样式时出现,这是我的脚本中第一个访问的样式。 When I override this method from HEADING1, the others work without problems.当我从 HEADING1 覆盖此方法时,其他方法可以正常工作。 Does anyone have any idea what may be going on?有谁知道可能发生了什么? I find it strange because the codes are exactly the same, except for the style formatting part.我觉得很奇怪,因为代码完全相同,除了样式格式部分。

My code:我的代码:

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

This is the result of my script when VerifiTitle1 () is enabled.这是启用 VerifiTitle1 () 时我的脚本的结果。

一跑

This is the result when VerifiTitle1 () is disabled.这是禁用 VerifiTitle1 () 时的结果。 第二轮

That is.那是。 With this method inaccessible.使用此方法无法访问。 everything goes well.一切顺利。

Modification points:修改点:

I think that the reason of your issue is to use for(var i = 0; i < paragrafs.length; i++) and removeChild() .我认为您的问题的原因是使用for(var i = 0; i < paragrafs.length; i++)removeChild() In this case, the child is removed from the top of Document body.在这种情况下,孩子会从文档正文的顶部移除。 By this, the children of Document body is changed.这样,Document body 的子节点就发生了变化。 So for example, how about the following modification?那么例如,下面的修改呢?

Modified script:修改后的脚本:

Please modify your script in verifiStyle() as follows.请在verifiStyle()中修改您的脚本,如下所示。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM