【问题标题】:้How to Copy Some Content of Google Document Table to Google Spreadsheet By Google App Script้如何通过 Google App Script 将 Google Document Table 的部分内容复制到 Google Spreadsheet
【发布时间】:2021-02-11 18:00:09
【问题描述】:

有什么方法可以选择谷歌文档表中的一些内容,通过谷歌应用脚​​本复制到谷歌电子表格中

跟随我的照片创意

我想将红框/蓝框中的一些内容复制到 Google 电子表格中,包括列名中的数据:级别、到期日 1、到期日 2

我从以下代码大纲开始:

function Tables() {
var doc = DocumentApp.getActiveDocument().getBody();  
var tables = doc.getTables()[1]; 
for (var i = 0; i < tables.getNumRows(); ++i) {
  var cell = tables.getRow(i);
  var text =cell.getText();
  Logger.log(text);
} 
}

【问题讨论】:

  • 为了正确了解您目前的情况,能否提供示例 Google 文档?
  • @Tanaike 谢谢你的关注,是的These is my Sample Google Document (output) 并且我附上了Sample Google Spreadsheet (input) 注意。将Doc的一些内容复制到电子表格
  • 感谢您回复并添加示例文档。虽然我看不到您的示例电子表格,但当我看到您的示例文档时,您当前的示例文档似乎与您问题中的示例图像不同。所以我现在很困惑。我为此道歉。在这种情况下,即使我提出了一个示例脚本,它也可能不是您期望的结果。对此我深表歉意。
  • @Tanaike,再次感谢您,我很抱歉在等待您检查时更改格式,我将再次更改它,如果您不这样做,我也附上Sample Spreadsheet不介意你能推荐我吗,
  • 感谢您的回复。虽然很遗憾,我无法从您的示例 Google 文档和示例输出图像中打开您的示例电子表格,但我为您的示例 Google 文档提出了一个示例脚本。你能确认一下吗?此示例脚本可用于您的示例 Google 文档。如果您的 Document 的结构发生更改,则可能需要修改脚本。所以请注意这一点。 当您测试此脚本时,请使用您的示例 Google 文档。

标签: google-apps-script google-sheets google-docs


【解决方案1】:

虽然我看不到您的示例电子表格,但从您的示例 Google 文档和示例输出图像中,我相信您的目标如下。

  • 您想从 Google 文档中的第一个表中检索值,并通过如下转换值将这些值放入 Google 电子表格。

    • 来自

修改点:

  • 在您的脚本中,检索到第二个表。在您的示例文档中,发生了错误。
  • 在您的情况下,我认为需要从每个单元格中检索值。但在您的脚本中,这些值是直接从行中检索的。
  • 在您的脚本中,仅从行中检索值。为了实现您的目标,需要将检索到的值从 Document 转换为 Spreadsheet。

当以上几点反映到脚本中时,它变成如下。

修改脚本:

请将以下脚本复制并粘贴到脚本编辑器并设置变量并运行脚本。关于excludeTexts 的变量,在这种情况下,这些值来自您的示例 Google 文档。当您更改文档表中的排除文本行时,请修改值。

function myFunction() {
  const documentId = "###";  // Please set Document ID.
  const spreadsheetId = "###";  // Please set Spreadsheet ID of the destination Spreadsheet.
  const sheetName = "Sheet1";  // Please set the sheet name of the destination sheet.
  const excludeTexts = ["Part A:", "Part B:", "Part C:"];  // Please set the exclude texts.

  // 1. Retrieve values from 1st table from Document and create an array for putting to Spreadsheet.
  // const body = DocumentApp.getActiveDocument().getBody(); // If you copy and paste the script to the container-bound script of Document, you can use this line instead of "DocumentApp.openById(documentId).getBody()".
  const body = DocumentApp.openById(documentId).getBody();
  const table = body.getTables()[0];
  let values = [];
  for (let r = 0; r < table.getNumRows(); r++) {
    const cell = table.getRow(r);
    const row = [];
    for (let c = 0; c < cell.getNumCells(); c++) {
      const text = cell.getCell(c).getText().trim();
      row.push(r > 0 && c == 1 ? text.split("\n").map(e => e.trim()).filter(e => !excludeTexts.includes(e)) : text);
    }
    values = values.concat(Array.isArray(row[1]) ? row[1].map((e, i) => i == 0 ? [row[0], e, ...row.slice(2)] : ["",e,...Array(row.slice(2).length).fill("")]) : [row]);
  }
  console.log(values)

  // 2. Put the created array to Spreadsheet.
  const sheet = SpreadsheetApp.openById(spreadsheetId).getSheetByName(sheetName);
  sheet.getRange(1, 1, values.length, values[0].length).setValues(values);
}
  • 为您的 Google 文档运行上述脚本时,将获得顶部图像的示例情况。

注意:

  • 此示例脚本可用于您的示例 Google 文档。如果您的 Document 的结构发生更改,则可能需要修改脚本。所以请注意这一点。当您测试此脚本时,请使用您的示例 Google 文档。

参考资料:

【讨论】:

  • 感谢您的帮助,我正在尝试测试您修改后的脚本,然后我得到了按照我的目标的结果再次感谢
  • @Vash Kris 欢迎您。谢谢你让我知道。我很高兴你的问题得到了解决。如果您的问题得到解决,请按接受按钮。与您有相同问题的其他人也可以将您的问题作为可以解决的问题。我认为您的问题和解决方案将对他们有用。如果找不到按钮,请随时告诉我。 stackoverflow.com/help/accepted-answer
  • 如果您不介意,请您解释一下为什么当脚本运行时发现一些包含编号 [1.] / 子弹 [.] [例如 1.你喜欢苹果吗?] 它没有将编号/项目符号检索到 Google 表格和其他一个当它找到编号 / 项目符号时,为什么它用单独的行而不是与检索的内容组相同的内容将内容检索到 Google 表格将内容添加到同一行的 Google 表格中再次感谢您
  • @Vash Kris 我必须为我糟糕的英语水平道歉。不幸的是,从您的回复中,我无法理解您的新问题。我可以问你关于你的新问题的细节吗?通过这个,我想尝试理解它。我再次为我糟糕的英语水平深表歉意。
  • @Tanaki 抱歉,我的问题不清楚,我附上了包含问题的图片,您能否看到它Question 如果您看不到它,请通知我再次感谢您
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多