2015-06-22 95 views
0

我需要一個將用作模板的另一組Docs內容複製到當前Doc的解決方案。 (我們需要保留當前的docId)。我編寫了一個簡單的Google Doc插件,它在一些Google Apps域中像魅力一樣工作。Google Apps腳本/驅動器API appendParagraph值插入兩次

我在四個不同的域中安裝(並製作了屏幕錄像)IDENTICAL代碼。在兩個域中,從所選「模板」文檔複製的內容在當前文檔中被(正確)複製一次,但是在另外兩個域中,它被注入TWICE! 所有域都具有相同的設置(以admin身份運行腳本,接受授權等)。我甚至在故障域中安裝了第二個副本,並顯示出相同的行爲。

任何人看到這種情況?

function runInsert(template) { 
    var targetDoc = DocumentApp.getActiveDocument(); 
    if(targetDoc.getHeader() == null) { 
    targetDoc.addHeader(); 
    }/ 
    if(targetDoc.getFooter() == null) { 
    targetDoc.addFooter(); 
    } 
    var templateDoc = DocumentApp.openById(template); 
    // check for header, get elements and add to current doc 
    var templateHeader = templateDoc.getHeader(); 
    if(templateHeader != null) { 
    var totalElementsHeader = templateHeader.getNumChildren(); 
    for(var j = 0; j < totalElementsHeader; ++j) { 
     var header = targetDoc.getHeader(); 
     var element = templateHeader.getChild(j).copy(); 
     var type = element.getType(); 
     if(type == DocumentApp.ElementType.PARAGRAPH){ 
     header.appendParagraph(element); 
     } 
    } 
    } 
    // check for body, get elements and add to current doc 
    var templateBody = templateDoc.getBody(); 
    if(templateBody != null) { 
    var totalElementsBody = templateBody.getNumChildren(); 
    for(var j = 0; j < totalElementsBody; ++j) { 
     var body = targetDoc.getBody(); 
     var element = templateBody.getChild(j).copy(); 
     var type = element.getType(); 
     if(type == DocumentApp.ElementType.PARAGRAPH){ 
     body.appendParagraph(element); 
     Logger.log("j is " + j + " element contains " + element.getText()); 
     } 
     else if(type == DocumentApp.ElementType.TABLE){ 
     body.appendTable(element); 
     .... 
    } 
    } 
    ... copy footer 
} 

回答

0

腳本是否運行已安裝的觸發器?我遇到了一個類似的問題,我的一個腳本中偶然安裝了兩次觸發器,導致腳本每次運行兩次。

檢查您的觸發器,並查看是否在腳本編輯器中爲您的項目安裝了倍數,位於資源>當前項目的觸發器

+0

不,沒有觸發器。從Test作爲加載項運行,或作爲已發佈的加載項運行,但在某些域中它可以正常工作,而在另一些域中,值將被複制兩次(2個域OK,2個域NOK)。 –

+0

您是否驗證腳本是否被調用了兩次或者它是否僅生成雙輸出? (使用附加組件和html輸出,你不能真正有效地使用記錄器,因爲它是asyncronus,每次調用服務器端函數時都會重置。)聽起來好像你的腳本被調用了兩次。你能提供你的任何代碼嗎? – Brady

+0

我檢查執行日誌和日誌,我看到只有一個appendParagraph線爲正文。 –

1

發現插入執行兩次,並修復它在側邊欄的JavaScript。