在文档中创建一个 table,其中多个单元格位于另一个单元格之前

create a table with multiple cells in front of another in a document

我需要创建一个table和图片中的一样,我已经安排好了数据

我只成功创建了简单的 tables。

一个示例数据数组,以及我目前的代码:

employees=[{    
  'factor':'Clientes',
  'caracteristica':'Tipo de Persona',
  'descripcion':'Posibilidad de que las personas naturales, juridicas..'
},{
  'factor':'Clientes',
  'caracteristica':'Tipo de Regimen',
  'descripcion':'Posibilidad de que los clientes incluidos...'
},{    
  'factor':'Clientes',
  'caracteristica':'Actividad Economica',
  'descripcion':'Posibilidad de que los clientes...'
},{
  'factor':'Productos y/o Servicios',
  'caracteristica':'Servicios',  
  'descripcion':'Posibilidad de que los distintos servicios ofrecidos '
},{
  'factor':'Productos y/o Servicios',
  'caracteristica':'Canales de distribucion',  
  'descripcion':'Posibilidad de que los canales....'
},{
  'factor':'Zona Geografica',
  'caracteristica':'Zona de Frontera',  
  'descripcion':'Presencia en Zonas de frontera con incidencias............'
},{
  'factor':'Zona Geografica',
  'caracteristica':'Zona de Produccion de Hoja de Coca',  
  'descripcion':'Presencia en Zonas para la produccion de hoja de coca.........'
},{
  'factor':'Zona Geografica',
  'caracteristica':'Zona de Minera',  
  'descripcion':'Presencia en Zonas con actividades...'
}];

function funcion2(employees) {
  var doc = DocumentApp.openById('some id');
  var nombre = ''
  var body = doc.getBody();
  var header = body.appendParagraph("Anexo 01");
  header.setAlignment(DocumentApp.HorizontalAlignment.CENTER)
    .editAsText().setFontSize(12);

  var header2 = body.appendParagraph("IDENTIFICACIÓN DE LOS RIESGOS DE LAFT Y RIESGOS ASOCIADOS");
  header2.setAlignment(DocumentApp.HorizontalAlignment.CENTER);
  body.appendParagraph("");
  var section = body.appendParagraph("Para identificar los riesgos de LAFT y riesgos asociados, de acuerdo con la clasificación y caracterización " + nombre + "-" + descripcion);
  section.setAlignment(DocumentApp.HorizontalAlignment.JUSTIFY);
  body.appendParagraph("");

  var tableCliente = body.appendTable();
  var tr1 = tableCliente.appendTableRow();
  var tb = tr1.appendTableCell("CLIENTE").setBackgroundColor("#84cdff");

  var table = body.appendTable();
  for (var i = 0; i < employees.length; i++) {
    if (employees[i].factor == "CLIENTES") {
      var tr=table.appendTableRow();
      var tb = tr.appendTableCell(employees[i].caracteristica).setWidth(90);
      var tb = tr.appendTableCell(employees[i].descripcion);
    }
  }
}

我不确定您为什么要使用脚本来执行此操作。我认为在页面编辑器中创建 table 会简单得多。

  • 打开一个空白文档。
  • 从主菜单中,Select插入,Table,并指定 3 列和 9 行。
  • 在第一行的所有三列中键入标题。
  • 在第二列和第三列中键入所有其他行的内容。
  • 在第1列中,select第2、3、4个单元格;右键单击并选择“合并单元格”
  • 在第 1 列中,select 第 5 和第 6 个单元格;右键单击并选择“合并单元格”
  • 第1列,select第7、8、9个单元格;右键单击并选择“合并单元格”。
  • 在第 1 列的三个合并单元格中输入内容。
  • Select任意单元格,右击,选择“Table属性”。在单元格垂直对齐下,select“中间”。
  • Select 第 1 行并更改背景颜色以适合。

脚本选项

OP 想通过脚本 创建一个table ,其特点是合并第一列中的某些"common" 单元格。 OP 的代码达到了 table 的 header。

在 Google 文档中创建 table 时,没有太多可用的资源。此外,很明显,对 table 强调 "row-wise" 方面和 events/commands 基于专栏的引用供不应求。

table内容的构建有两种选择。在此代码中,我遵循了 OP 循环遍历 Object 并使用内容逐步更新 table 的方法。另一种方法是构建一个新数组,然后按照 Google Class Table documentation.

中描述的大致相同的方式从该数组构建 table
employees = [{
  'factor': 'Clientes',
  'caracteristica': 'Tipo de Persona',
  'descripcion': 'Posibilidad de que las personas naturales, juridicas..'
}, {
  'factor': 'Clientes',
  'caracteristica': 'Tipo de Regimen',
  'descripcion': 'Posibilidad de que los clientes incluidos...'
}, {
  'factor': 'Clientes',
  'caracteristica': 'Actividad Economica',
  'descripcion': 'Posibilidad de que los clientes...'
}, {
  'factor': 'Productos y/o Servicios',
  'caracteristica': 'Servicios',
  'descripcion': 'Posibilidad de que los distintos servicios ofrecidos '
}, {
  'factor': 'Productos y/o Servicios',
  'caracteristica': 'Canales de distribucion',
  'descripcion': 'Posibilidad de que los canales....'
}, {
  'factor': 'Zona Geografica',
  'caracteristica': 'Zona de Frontera',
  'descripcion': 'Presencia en Zonas de frontera con incidencias............'
}, {
  'factor': 'Zona Geografica',
  'caracteristica': 'Zona de Produccion de Hoja de Coca',
  'descripcion': 'Presencia en Zonas para la produccion de hoja de coca.........'
}, {
  'factor': 'Zona Geografica',
  'caracteristica': 'Zona de Minera',
  'descripcion': 'Presencia en Zonas con actividades...'
}];

function so5501193401() {

  //define header cell style which we will use while adding cells in header row
  //Background color, text bold, white
  var headerStyle = {};
  headerStyle[DocumentApp.Attribute.BACKGROUND_COLOR] = '#84cdff';
  headerStyle[DocumentApp.Attribute.BOLD] = true;
  headerStyle[DocumentApp.Attribute.FOREGROUND_COLOR] = '#FFFFFF';

  //Style for the cells other than header row
  var cellStyle = {};
  cellStyle[DocumentApp.Attribute.BOLD] = false;
  cellStyle[DocumentApp.Attribute.FOREGROUND_COLOR] = '#000000';

  //By default, each paragraph had space after, so we will change the paragraph style to add zero space
  //we will use it later
  var paraStyle = {};
  paraStyle[DocumentApp.Attribute.SPACING_AFTER] = 0;
  paraStyle[DocumentApp.Attribute.LINE_SPACING] = 1;

  //setup the document reference
  var docid = "<<insert document id as a string>>";
  var doc = DocumentApp.openById(docid);
  var body = doc.getBody();

  // build the document introduction
  var header = body.appendParagraph("Anexo 01");
  header.setAlignment(DocumentApp.HorizontalAlignment.CENTER).editAsText().setFontSize(12);
  var header2 = body.appendParagraph("IDENTIFICACIÓN DE LOS RIESGOS DE LAFT Y RIESGOS ASOCIADOS");
  header2.setAlignment(DocumentApp.HorizontalAlignment.CENTER);
  body.appendParagraph("");
  var section = body.appendParagraph("Para identificar los riesgos de LAFT y riesgos asociados, de acuerdo con la clasificación y caracterización");
  section.setAlignment(DocumentApp.HorizontalAlignment.JUSTIFY);
  body.appendParagraph("");

  // variables for the loop
  var factortext = "";
  var factorcounter = 0

  // build the header row of the table
  var table = body.appendTable();
  var tr = table.appendTableRow();
  var col1title = "Factores de riesgo";
  var col2title = "Caracteristica";
  var col3title = "Descripcion del Riesgo de LAFT";
  var th = tr.appendTableCell(col1title);
  th.setAttributes(headerStyle);
  var th = tr.appendTableCell(col2title);
  th.setAttributes(headerStyle);
  var th = tr.appendTableCell(col3title);
  th.setAttributes(headerStyle);

  // create a loop to work through the 'employees' object
  for (var i = 0; i < employees.length; i++) {

    // add a row and define the cell contents
    var tr = table.appendTableRow();
    var factorname = employees[i].factor;
    var caraname = employees[i].caracteristica;
    var descname = employees[i].descripcion;

    //establish if this is the first row or not
    if (factorcounter != 0) {
      // Logger.log("DEBUG: i: "+i+", record#"+(i+1));//DEBUG
      var factorname = employees[i].factor;
      // Logger.log("DEBUG: factor: "+factorname);//DEBUG

      // establish if this is the continuation or beginning of a new factor group
      if (factortext != factorname) { // this is a new group
        var td = tr.appendTableCell(factorname);
        td.setAttributes(cellStyle);
      } else {
        var td = tr.appendTableCell("");
      }

      factortext = factorname;
    }

    // establish if this is the first row of the table
    if (factorcounter == 0) {
      // Logger.log("DEBUG: i: "+i+", record#1");//DEBUG
      var factorname = employees[i].factor;
      factortext = factorname;
      //Logger.log("DEBUG: factor: "+factorname);//DEBUG
      var td = tr.appendTableCell(factorname);
      td.setAttributes(cellStyle);
    }

    // increment the factor counter
    factorcounter++;

    // insert the characttics and description
    var td = tr.appendTableCell(caraname);
    td.setAttributes(cellStyle);
    var td = tr.appendTableCell(descname);
    td.setAttributes(cellStyle);

  }

}

文档截图