正如標題所示,我正在嘗試在 google 檔案表中查找占位符文本元素的索引,以便可以將其替換為存盤在不同檔案中的文本。
我可以使用我在stackoverflow上找到的其他示例將預格式化的文本添加到檔案中 - 而不是表格中所需的位置。
但是,我不清楚如何在模板檔案中的許多表之一中找到占位符元素的索引。
我需要占位符文本的索引才能使用 insertParagraph 函式。
添加更多細節:我能夠找到占位符文本并使用以下代碼插入影像。
function replaceTextToImage(body, searchText, image, width) {
var next = body.findText(searchText);
if (!next) return;
var r = next.getElement();
r.asText().setText("");
var img = r.getParent().asParagraph().insertInlineImage(0, image);
if (width && typeof width == "number") {
var w = img.getWidth();
var h = img.getHeight();
img.setWidth(width);
img.setHeight(width * h / w);
}
return next;
};
但是,我需要保留要匯入的檔案的格式。因此,我打開帶有格式化文本的檔案,然后使用條件回圈遍歷不同的元素型別,如果元素型別匹配,則插入文本/影像。這就是為什么我需要占位符文本的索引。請參閱以下功能:
function replaceTextWithDoc(body, searchText, id) {
let doc = DocumentApp.openById(id)
var numElements = body.getNumChildren();
var index = numElements;
for (var i = 0; i < numElements; i ) {
var child = body.getChild(i);
if (child.asText().getText() == searchText){
index = i;
body.removeChild(child);
break;
}
}
var totalElements = doc.getNumChildren();
for( var j = 0; j < totalElements; j ) {
var element = doc.getChild(j).copy();
var type = element.getType();
if( type == DocumentApp.ElementType.PARAGRAPH )
body.insertParagraph(index, element);
else if( type == DocumentApp.ElementType.TABLE )
body.insertTable(index, element);
else if( type == DocumentApp.ElementType.LIST_ITEM )
body.insertParagraph(index, element);
else
throw new Error("According to the doc this type couldn't appear in the body: " type);
}
}
以下是表格中占位符文本 ( {iText} ) 的示例:https ://docs.google.com/document/d/1mZWpQqk4gYAF6UCRALrT8S99-01RYNfwni_kqDzOg7E/edit?usp=sharing
這是我需要替換占位符文本的文本和影像示例 - 保持所有/任何格式。https://docs.google.com/document/d/1wuX0g5W2GL0YJ7admiv3TNEepb_zVwQleKwazCiAMBU/edit?usp=sharing
uj5u.com熱心網友回復:
問題:
如果我理解正確,您希望將一個檔案(由文本和內嵌影像組成)的內容復制到包含特定占位符文本的特定表格單元格中。
解決方案:
在這種情況下,我建議如下:
- 使用Body.getTables() 遍歷目標檔案中的所有表。
- 對于每個表,遍歷其所有單元格。
- 對于每個單元格,檢查其文本是否包含占位符。
- 如果包含占位符,請通過TableCell.clear()清除當前單元格內容。
- 遍歷所有源檔案元素(例如,參見這個答案)。
- 對于每個元素,檢查其型別(Element.getType())。
- 如果元素是段落,則通過TableCell.appendParagraph將該段落附加到單元格中。
- 如果元素是影像,則通過TableCell.appendImage附加它。
代碼示例:
const PLACEHOLDER = "{iText}";
function myFunction() {
const doc = DocumentApp.openById(TARGET_ID);
const sourceDoc = DocumentApp.openById(SOURCE_ID);
const body = doc.getBody();
const tables = body.getTables();
tables.forEach(table => {
const numRows = table.getNumRows();
for (let i = 0; i < numRows; i ) {
const row = table.getRow(i);
const numCells = row.getNumCells();
for (let j = 0; j < numCells; j ) {
const cell = row.getCell(j);
const cellText = cell.editAsText();
const text = cellText.getText();
if (text.includes(PLACEHOLDER)) {
cell.clear();
appendSourceContent(sourceDoc, cell);
}
}
}
});
}
function appendSourceContent(doc, cell) {
const numChildren = doc.getNumChildren();
for (let j = 0; j < numChildren; j ) {
const element = doc.getChild(j).copy();
const type = element.getType();
if (type == DocumentApp.ElementType.PARAGRAPH) {
cell.appendParagraph(element);
} else if (type == DocumentApp.ElementType.INLINE_IMAGE) {
cell.appendImage(element);
}
}
}
筆記:
else if如果源內容可以具有與ElementTypes段落和行內影像不同的內容,請添加其他塊。- 我假設您不想保留表格單元格中的任何當前內容。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/466573.html
