2016-05-13 29 views
0

我想將GDoc的內容複製到另一個GDoc。這適用於所有不同的元素類型。包括一個表格(Enum DocumentApp.ElementType.TABLE)。但是,如果該表包含嵌入式圖像(Enum DocumentApp.ElementType.INLINE_IMAGE),則圖像無法正確複製。應用程序腳本谷歌文檔複製表與嵌入式圖像

以下是源代碼GDoc示例的鏈接。 https://docs.google.com/document/d/14kXjC0CTkEgmD7Ttv0YKL9ubfDRHbL1hCVOqOiyiEDU/edit#。用新西蘭國旗找到表格中的那一行。該標誌未正確複製到目標GDoc的新表中。

我只是在源文檔(上面)中找到表格對象並使用Body :: insertTable(childIndex,table)將其插入到目標GDoc的主體中。表中大多數其他元素複製OK。包括嵌入式Google繪圖。但不是內聯圖像。

回答

0

我有一個類似的問題,在我的情況下,編號列表的GLYPH_TYPE也丟失了。 但我發現,如果CellChildren被逐一拷貝,圖像將被很好地複製。

所以我解決了我的問題,在整個複製新表,然後從原始表中逐一替換每個dstTable單元格的內容。

即使嵌套表格也能正常工作,因爲如果檢測到表格中的其他表格,函數會調用它自己。 而且,通過在插入之後另外設置屬性,解決了丟失的ListItem屬性的問題。

這裏是我的好工作代碼段:

首先我檢測表,並在dstBody插入...

... 
dstChild = srcChild.copy(); 
switch(dstChild.getType()) { 
    case DocumentApp.ElementType.PARAGRAPH: 
    ... 
    case DocumentApp.ElementType.LIST_ITEM: 
    ... 
    case DocumentApp.ElementType.TABLE: 
    var newTable = 
    dstBody.insertTable(dstBody.getNumChildren()-1, dstChild); 
    copyTableCellByCell(dstChild, newTable); 
    break; 
.... 
} 

這是可能的遞歸函數,首先替換每個細胞清除它並複製原表中的內容:

function copyTableCellByCell(srcTable, dstTable) { 
    var numRows = dstTable.getNumRows(); 
    var dstRow, numCells, dstCell, actCellIndex; 
    for (var actRowIndex = 0; actRowIndex < numRows; actRowIndex++) { 
    dstRow = dstTable.getRow(actRowIndex); 
    numCells = dstRow.getNumCells(); 
    for (actCellIndex = 0; actCellIndex < numCells; actCellIndex++) { 
     dstCell = dstRow.getCell(actCellIndex); 
     dstCell.clear(); 
     var srcCell = srcTable.getCell(actRowIndex, actCellIndex); 
     var numCellChildren = srcCell.getNumChildren(); 
     for (var y = 0; y < numCellChildren; y++) { 
     var cellChild = srcCell.getChild(y); 
     var childCopy = cellChild.copy(); 
     switch(childCopy.getType()) { 
      case DocumentApp.ElementType.PARAGRAPH: 
      dstCell.insertParagraph(y, childCopy); 
      break; 
      case DocumentApp.ElementType.LIST_ITEM: 
      // that the GLYPH_TYPE doesn't get lost 
      var atts = childCopy.getAttributes(); 
      var 
      newListItem = dstCell.insertListItem(y, childCopy); 
      newListItem.setAttributes(atts); 
      break; 
      case DocumentApp.ElementType.TABLE: 
      var newTable = 
      dstCell.insertTable(y, childCopy); 
      copyTableCellByCell(cellChild, newTable); 
      break; 
     } 
     } 
     // remove the very first empty paragraph in the cell 
     while ((y = dstCell.getNumChildren()) > numCellChildren) { 
     dstCell.getChild(y - 1).removeFromParent(); 
     } 
    } 
    } 
} 

當然,這可以進行微調。 如果您希望服務器不必做更少的工作,您可以搜索並挑選內嵌圖像。

我希望,這有助於任何方式。 非常感謝您對我們的關注,

理查德

0

我發現了一個沒有編碼解決這個bug。

將圖像作爲「繪圖」插入源文檔中。

  1. 單擊需要插入圖像的表格單元格。
  2. 單擊文檔菜單上的「插入」。
  3. 點擊「插入圖紙」。
  4. 在繪圖窗格中添加要插入的圖像。
  5. 保存並關閉。

結果將是表中的圖像,與表格類型一起被完美複製。

相關問題