带有电子表格分组表的文档

问题描述 投票:0回答:1

我正在编写一个脚本,该脚本将从包含两列的电子表格中提取数据:

  • 公司
  • 产品

我想基于 Google 文档模板创建一个新文档,其中包含公司和产品的变量。对于电子表格中列出的每个公司,产品应列在公司下方。例如:

公司1
产品1
产品2
公司2
产品A
产品B

我整理了以下脚本:

function generateDocumentFromSheet() {
  // Open the Google Sheet
  var sheet = SpreadsheetApp.openById('YY');
  var dataSheet = sheet.getSheetByName('Data');

  // Get the data
  var data = dataSheet.getRange('A2:B' + dataSheet.getLastRow()).getValues(); 

  // Group Products by Company
  var companyShowMap = {};
  data.forEach(function(row) {
    var companyName = row[1]; // Assuming Company column is the second column (index 1)
    var product = row[0]; // Assuming Product column is the first column (index 0)
    if (!companyShowMap[companyName]) {
      companyShowMap[companyName] = [];
    }
    companyShowMap[companyName].push(product);
  });

  // Open the template Google Doc
  var templateDoc = DocumentApp.openById('XX');
  var body = templateDoc.getBody();

  // Replace placeholders with grouped data
  for (var company in companyShowMap) {
    // Duplicate the table section for each company
    var tableSection = body.copy();
   
    // Replace placeholders in the table section
    tableSection.replaceText('[[company]]', company);
   
    // Add rows for each Product
    var products = companyShowMap[company];
   products.forEach(function(product) {
      var newRow = tableSection.copy();
      newRow.replaceText('[[product]]', product);
      body.appendTable(newRow);
    });
  }

  // Create a new document with populated data
  var newDoc = DocumentApp.create('Populated Document'); // Change the title as desired
  var newDocBody = newDoc.getBody();
  newDocBody.appendParagraph(body.getText());

  // Save and close the new document
  newDoc.saveAndClose();
}

我在“为每个产品添加行”部分中收到以下错误:

错误
异常:参数 (DocumentApp.Body) 与 DocumentApp.Body.appendTable 的方法签名不匹配。

google-sheets google-apps-script google-docs
1个回答
0
投票

本质上,您得到的错误是由于混合类型造成的。在你的代码中

body.appendTable(newRow);

“newRow”实际上是一个正文文档元素,它旨在成为 Google 文档内任何其他元素(例如段落、表格、图像等)的父元素。

由于您的函数还存在一些其他潜在问题,因此我重写了我个人如何制作这个坏男孩的方法。流程中最大的变化是使模板保持不变,但复制模板并编辑该文档。

这个功能应该可以做到。您将能够在模板中更改您喜欢的格式,并让“填充文档”反映公司行和产品行的行格式。

 function generateDocumentFromSheet() {
  const ss = SpreadsheetApp.openById('1ua9-f5flQd2GlP1-6_PiajL4DTppwNQtPMzQqRiomOw');
  const dataSheet = ss.getSheetByName('Data');
  const data = dataSheet.getRange('A2:B' + dataSheet.getLastRow()).getValues();
  const companyShowMap = {};
  const templateDocId = "15pufbbYFX9krBWR_VbNyF2d8lsP3rftCaj0zJDYIZNk";
  const focusDoc = DocumentApp.openById(DriveApp.getFileById(templateDocId).makeCopy("Populated Document").getId());
  const body = focusDoc.getBody();
  const [table] = body.getTables();
  const companyRow = table.getRow(0);
  const productRow = table.getRow(1);
  const companyIncrementer = 1;

  data.forEach(row => {
    const [product, companyName] = row;

    if (!companyShowMap[companyName]) companyShowMap[companyName] = [];

    companyShowMap[companyName].push(product);
  });

  for (const company in companyShowMap) {
    // Duplicate the table section for each company to append rows to
    const companyTable = table.copy()
    const products = companyShowMap[company];

    companyTable.appendTableRow(companyRow.copy()).replaceText('\\[\\[company\\]\\]', company);

    // Add rows for each Product
    products.forEach(product => companyTable.appendTableRow(productRow.copy()).replaceText('\\[\\[product\\]\\]', product));

    //clean up the table by removing the initial 2 rows
    companyTable.removeRow(0);
    companyTable.removeRow(0);

    body.appendTable(companyTable)
  };

  //remove the initial table
  body.removeChild(table);

  focusDoc.saveAndClose();
}

当然,把ID替换成你自己的👍

© www.soinside.com 2019 - 2024. All rights reserved.