DocumentApp SaveAndClose()throw服务不可用:文档

时间:2015-03-24 09:33:31

标签: google-apps-script

此代码创建文档,将其复制到文件夹中,添加文本和图像,没有页脚。 当我关闭并保存文档时,通常(并非总是)我有:"服务不可用:文档"。

//create a Google Doc
var rootDoc = DocumentApp.create(docName); 
var idDoc = rootDoc.getId();
var origDoc = DriveApp.getFileById(idDoc);
//move to folder
var newFile =  origDoc.makeCopy(docName, folder);
DriveApp.removeFile(origDoc);
//open
var newDoc = DocumentApp.openById(newFile.getId());
var body = newDoc.getBody();
body.setMarginTop(40);
body.setMarginBottom(40);

//IMG HEADER DOC
var logo = UrlFetchApp.fetch(PROP.getProperty("URL_LOGO_OVS")).getBlob();
body.appendImage(logo).setHeight(40).setWidth(87);

body.appendParagraph('My File').setHeading(DocumentApp.ParagraphHeading.HEADING1).setAlignment(DocumentApp.HorizontalAlignment.CENTER);
body.appendHorizontalRule();

//IMAGE
var tab2 = body.appendTable([
    ['','']
  ]).setBorderWidth(0).setAttributes(textStyle);

 tab2.getCell(0,0).appendParagraph('FOCAL').setHeading(DocumentApp.ParagraphHeading.HEADING2);
  tab2.getCell(0,0).appendParagraph('Rispetto del criterio espositivo: '+theForm.valBrFoc);
  if (theForm.noteFocal != null && theForm.noteFocal != '') { tab2.getCell(0,0).appendParagraph('Note: '+theForm.noteFocal); }

  var inline2 = tab2.getCell(0,1).appendImage(img2).setLinkUrl("https://drive.google.com/uc?export=view&id="+img2.getId());
  if (tab2.getCell(0, 1).getChild(1) != null) {
tab2.getCell(0, 1).getChild(1).asParagraph().setAlignment(DocumentApp.HorizontalAlignment.RIGHT); 
  }
  inline2.setWidth(150).setHeight(100);

//TEXT
body.appendParagraph('VETRINA: non presente.').setHeading(DocumentApp.ParagraphHeading.HEADING2); 

//this throw error: Service unavailable: Docs (blank document)
newDoc.saveAndClose();

1 个答案:

答案 0 :(得分:0)

我发现了问题!在我的表单(HTML客户端)中,有一个textarea ..所以..当用户点击返回时,字符串里面有\ n \ r!但addParagraph说\ n不支持\ n! 我解决了textArea的文本替换:

str.replace(new RegExp('\r?\n','g'), '\r');