![](/img/trans.png)
[英]How can I have a cell that when edited publishes it's edit date next to it? In Google Sheets using a Google Apps Script
[英]How to copy and edit text data inside a table cell in Google Documents using Google Apps Script
我是 Google Apps 脚本的新手,并且已经被这个问题困扰了好几天。 提前感谢那些试图帮助我的人。
我正在尝试从某个表格单元格中复制文本数据,用换行符将它们分开并将它们放入变量中,然后在另一个表格中使用它们。 使用 tablecell.getText() 你会丢失所有格式,所以我想使用 Paragraphs,但是使用表格单元格你不能使用 getParagraphs()...
tableCellOut.appendParagraph(tableIn.getRow(1).getChild(1).asParagraph());
我不知道我离目标有多近。 有没有一种方法可以在不丢失格式的情况下编辑文本数据?
我相信你的目标如下。
您希望使用 Google Apps 脚本将表格单元格转换为问题中显示的图像。 (下图来自您的问题。)
为此,这个答案怎么样? 我想提出以下示例脚本来解决您的问题。 该脚本的流程如下。
请将以下脚本复制并粘贴到 Google Document 的容器绑定脚本中,并在脚本编辑器中运行myFunction
的 function。 在此脚本中, row
和column
分别是行号和列号。 因此,在您的图像中,请设置row = 2
和column = 2
。 例如,当您要拆分单元格“C3”时,请设置row = 3
和column = 3
。
function myFunction() {
// 1. Retrieve table.
const body = DocumentApp.getActiveDocument().getBody();
const table = body.getTables()[0];
// 2. Retrieve the cell "B2" of the table.
const row = 2; // Please set the row number.
const column = 2; // Please set the column number.
const cell = table.getCell(row - 1, column - 1);
// 3. Create an object including the text and text styles. This is used for splitting values to the cells.
const text = cell.editAsText();
let temp = [];
const textRuns = [].reduce.call(text.getText(), (ar, c, i, a) => {
if (c != "\n") temp.push({text: c, foregroundColor: text.getForegroundColor(i), backgroundColor: text.getBackgroundColor(i), textAlignment: text.getTextAlignment(i), italic: text.isItalic(i)});
if (c == "\n" || i == a.length - 1) {
ar.push({text: temp.reduce((s, {text}) => s += text, ""), styles: temp});
temp = [];
}
return ar;
}, []);
// 4. The text and text styles are set to the cells.
for (let i = row - 1; i < table.getNumRows(); i++) {
const t = table.getCell(i, column - 1).editAsText();
const run = textRuns.shift();
t.setText(run.text);
run.styles.forEach((r, j) => {
t.setBackgroundColor(j, j, r.backgroundColor);
t.setForegroundColor(j, j, r.foregroundColor);
t.setItalic(j, j, r.italic);
if (r.textAlignment) t.setTextAlignment(j, j, r.textAlignment);
});
}
// 5. When the number of rows are smaller than the number of rows of splitted values, the new rows are appended and the text and text styles are set to the cells.
while (textRuns.length > 0) {
const appendedRow = table.appendTableRow();
for (let i = 0; i < table.getRow(row - 1).getNumCells(); i++) {
appendedRow.appendTableCell("");
}
const t = appendedRow.getCell(column - 1).editAsText();
const run = textRuns.shift();
t.setText(run.text);
run.styles.forEach((r, j) => {
t.setBackgroundColor(j, j, r.backgroundColor);
t.setForegroundColor(j, j, r.foregroundColor);
t.setItalic(j, j, r.italic);
if (r.textAlignment) t.setTextAlignment(j, j, r.textAlignment);
});
}
}
当为您的初始表运行上述脚本时,可以获得以下结果。 在此演示中,首先展开单元格“B2”的值,然后展开单元格“C3”的值。
BackgroundColor
和ForegroundColor
用作文本 styles。 当您想使用其他文本 styles 时,请修改上面的脚本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.