如何使用Google Apps脚本在Google文档的表格单元内复制和编辑文本数据

米莎

我是Google Apps脚本的新手,已经被这个问题困扰了几天。在此先感谢那些正在寻求帮助的人。

我正在尝试从某个表单元格复制文本数据,将它们用换行符分隔,并将其放入变量中,然后在另一个表中使用它们。使用tablecell.getText()将会丢失所有格式,因此我想改用Paragraphs,但是使用表格单元格则无法使用getParagraphs()...

tableCellOut.appendParagraph(tableIn.getRow(1).getChild(1).asParagraph());

我不知道我离我的目标有多近。有没有一种方法可以编辑文本数据而不丢失格式?

这是表格前后的图像

Tanaike

我相信您的目标如下。

  • 您希望使用Google Apps脚本实现表格单元格转换为问题中所显示的图像。(下图来自您的问题。)

为此,这个答案如何?我想提出以下示例脚本来解决您的问题。该脚本的流程如下。

  1. 检索表。
  2. 检索表的单元格“ B2”。
    • 这来自您的示例图像。
  3. 创建一个包含文本和文本样式的对象。
    • 这用于将值拆分到单元格。
  4. 文本和文本样式设置为单元格。
  5. 当行数小于分割值的行数时,将追加新行,并将文本和文本样式设置为单元格。

示例脚本:

Please copy and paste the following script to the container-bound script of the Google Document, and run the function of myFunction at the script editor. In this script, row and column are the row and column numbers, respectively. So in your image, please set row = 2 and column = 2. For example, when you want to split the cell "C3", please set row = 3 and 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);
    });
  }
}

Result:

When above script is run for your initial table, the following result can be obtained. In this demonstration, at first, the values of cell "B2" are expanded, and then, the values of cell "C3" are expanded.

在此处输入图片说明

Note:

  • This sample script is prepared for above situation. If your specification of above image is changed, the script might not be able to be used. So please be careful this.
  • 在此示例脚本中,BackgroundColor并且ForegroundColor用作文本样式。如果您想使用其他文本样式,请修改上面的脚本。

参考文献:

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用Google Apps脚本从Google表格数据表复制到Google文档表

来自分类Dev

使用Google Apps脚本将图像从Google表格复制到Google文档

来自分类Dev

Google Apps脚本->将文档中的文本写入表格

来自分类Dev

如何将未经评估的文本数据写入Google表格单元格

来自分类Dev

使用Apps脚本将文本和图像添加到Google Doc中表格的特定单元格

来自分类Dev

使用Apps脚本返回到某些单元格并使其在Google表格中可编辑

来自分类Dev

如何使用Google Apps脚本在电子表格的单元格中剪切文本?

来自分类Dev

使用Google Apps脚本从Google文档中提取文本

来自分类Dev

需要Google Apps脚本和脚本数据库替换示例

来自分类Dev

使用Apps脚本删除Google表格中的空白行后,是否要复制数据?

来自分类Dev

如何使用Google脚本和Google表格执行Cartersian Join?

来自分类Dev

如何使用Google Apps脚本向电子表格单元格添加链接

来自分类Dev

如何使用Google Apps脚本向电子表格单元格添加链接

来自分类Dev

Google Apps脚本-将数据从电子表格追加到文档模板中的表格

来自分类Dev

如何使用Google Apps脚本替换电子表格中的文本?

来自分类Dev

如何使用 Google Apps 脚本在复制的数据范围旁边添加公式?

来自分类Dev

如何使用脚本在Google文档中插入文本并使其居中

来自分类Dev

在Apps脚本和Google表格中使用ImportHTML进行数据收集

来自分类Dev

Google Sheet Apps脚本:如何编辑下面的Apps脚本,以便仅将数据从A列复制到L列并将P列复制到S列?

来自分类Dev

使用Google脚本在单元格之间复制和粘贴数据

来自分类Dev

使用Google Apps脚本编辑Google电子表格

来自分类Dev

在Google Apps脚本中创建电子表格备份时,如何复制格式和值,而不是公式?

来自分类Dev

使用Google Apps脚本将Google表格中的图表正确插入文档中

来自分类Dev

在单元格中查找文本并将其替换为Google脚本和Google表格

来自分类Dev

如何通过Python使用Google Apps脚本?

来自分类Dev

使用Google Apps脚本在Google电子表格中插入单元格

来自分类Dev

如何使用Google Apps脚本阅读Google文档中的标题参考

来自分类Dev

Google表格+ Apps脚本:如何根据单元格的值按名称删除表格

来自分类Dev

Google Apps脚本编辑器中类似Javadoc的文档?

Related 相关文章

  1. 1

    使用Google Apps脚本从Google表格数据表复制到Google文档表

  2. 2

    使用Google Apps脚本将图像从Google表格复制到Google文档

  3. 3

    Google Apps脚本->将文档中的文本写入表格

  4. 4

    如何将未经评估的文本数据写入Google表格单元格

  5. 5

    使用Apps脚本将文本和图像添加到Google Doc中表格的特定单元格

  6. 6

    使用Apps脚本返回到某些单元格并使其在Google表格中可编辑

  7. 7

    如何使用Google Apps脚本在电子表格的单元格中剪切文本?

  8. 8

    使用Google Apps脚本从Google文档中提取文本

  9. 9

    需要Google Apps脚本和脚本数据库替换示例

  10. 10

    使用Apps脚本删除Google表格中的空白行后,是否要复制数据?

  11. 11

    如何使用Google脚本和Google表格执行Cartersian Join?

  12. 12

    如何使用Google Apps脚本向电子表格单元格添加链接

  13. 13

    如何使用Google Apps脚本向电子表格单元格添加链接

  14. 14

    Google Apps脚本-将数据从电子表格追加到文档模板中的表格

  15. 15

    如何使用Google Apps脚本替换电子表格中的文本?

  16. 16

    如何使用 Google Apps 脚本在复制的数据范围旁边添加公式?

  17. 17

    如何使用脚本在Google文档中插入文本并使其居中

  18. 18

    在Apps脚本和Google表格中使用ImportHTML进行数据收集

  19. 19

    Google Sheet Apps脚本:如何编辑下面的Apps脚本,以便仅将数据从A列复制到L列并将P列复制到S列?

  20. 20

    使用Google脚本在单元格之间复制和粘贴数据

  21. 21

    使用Google Apps脚本编辑Google电子表格

  22. 22

    在Google Apps脚本中创建电子表格备份时,如何复制格式和值,而不是公式?

  23. 23

    使用Google Apps脚本将Google表格中的图表正确插入文档中

  24. 24

    在单元格中查找文本并将其替换为Google脚本和Google表格

  25. 25

    如何通过Python使用Google Apps脚本?

  26. 26

    使用Google Apps脚本在Google电子表格中插入单元格

  27. 27

    如何使用Google Apps脚本阅读Google文档中的标题参考

  28. 28

    Google表格+ Apps脚本:如何根据单元格的值按名称删除表格

  29. 29

    Google Apps脚本编辑器中类似Javadoc的文档?

热门标签

归档