ZionEnglish
ZionEnglish

Reputation: 53

Changing Table's Line Spacing in Google App Script

I am trying to automatically generate a "quote" document. I am using tables to represent the fees associated with the quote, however, the default spacing for the table's rows are larger than necessary.

I have tried setting the "minimum height" for each row, but this does not seem to affect my tables.

I have also tried making an attribute that defines a 0.5 line-height, but this doesn't work either (I do not see any effect).

Any help would be greatly appreciated, thanks!

edit: here's my code

function generateDoc(purchasePrice,commission, hoa, other, recording, escrow, netProceeds) {
   
  //style for tables
  var tableStyle = {};
  tableStyle[DocumentApp.Attribute.LINE_SPACING] = 1;
  
  //----------------------------------------//
       //Quick Reference of Variables// 
  comapnyName        = "EscrowComapny";
  var superAA = {}; //append all the tables to this Associative Array, in order to properly display them
  superAA["Commission Charges"]         = commission;             // <== Associative Arrays
  superAA["H.O.A/Management"]           = hoa;
  superAA["Other Debits/Credits"]       = other;
  superAA["Title/Recording Charges"]    = recording;
  superAA["Escrow Charges"]             = escrow;             // <== END Associative Arrays
  superAA["Net Proceeds"]               = netProceeds;       //  <== Integer
  superAA["Total"]                      = purchasePrice;    //   <== Integer
  var address                           = "address";
  var date                              = "September 50th, 2089";
  //----------------------------------------//
  
  
  //-------------------------
  var boldRows = [];
  var boldCounter = 2;
  //-------------------------
  
  
  //Create document Name//
  
  //TESTING: To load the sandbox document
  var doc = DocumentApp.openByUrl('URL');  
  
  //get body of document, so we can append stuff to it
  var body = doc.getBody();
  body.setAttributes(tableStyle);
  
  //add the title card
  var title = body.appendParagraph("SELLER'S ESTIMATED SETTLEMENT STATEMENT");
  title.setAlignment(DocumentApp.HorizontalAlignment.CENTER);
  title.editAsText().setBold(true);
  title.editAsText().setFontSize(10);
  
  //add a line in between
  body.appendParagraph("\n");
  
  //add the sales info as a table 
  var dressDate = [['Property:', address, 'Date:', date]];
  var dressDateTable = body.appendTable(dressDate);
  //set the identifiers bold
  dressDateTable.getRow(0).getCell(0).editAsText().setBold(true);
  dressDateTable.getRow(0).getCell(2).editAsText().setBold(true);
  //set the info false
  dressDateTable.getRow(0).getCell(1).editAsText().setBold(false);
  dressDateTable.getRow(0).getCell(3).editAsText().setBold(false);
  //set the spacing 
  dressDateTable.getRow(0).getCell(1).setWidth(200);
  dressDateTable.getRow(0).getCell(0).setWidth(58);
  dressDateTable.getRow(0).getCell(2).setWidth(90);
  //set invisible line weight
  dressDateTable.setBorderWidth(0);
  
  //write table for header
  var tableData  = [["", "Debits", "Credits"]];
  
  //add sales price
  tableData.push(["Financial Consideration","",""],["        Total Consideration", "", formatCurrency(purchasePrice)]);
  
  //now dynamically add the other expenses
  
  for(key in superAA)
  {
    
    if(key == "Net Proceeds" || key == "Total")
    {
      tableData.push([key, formatCurrency(superAA[key]), ""]);
      boldCounter++;
      boldRows.push(boldCounter);
      continue;

    }
    //now add the title (mark this later for efficiency when bolding)
    tableData.push([key, "", ""]);
    boldCounter++;
    boldRows.push(boldCounter);
    
    //then, add the nested stuff under the debit section
    for(fee in superAA[key])
    {
      //these wont be bolded
      if(fee == "Discount")
      {
        tableData.push(["        " + fee, "", formatCurrency(superAA[key][fee])]);
      }
      else
      {
        tableData.push(["        " + fee, formatCurrency(superAA[key][fee]), ""]);
      }
      boldCounter++;
    }
  }
    
  var expenseTable = body.appendTable(tableData);
  
  //setColor 
  expenseTable.getRow(0).getCell(0).setBackgroundColor("#D3D3D3");
  expenseTable.getRow(0).getCell(1).setBackgroundColor("#D3D3D3");
  expenseTable.getRow(0).getCell(2).setBackgroundColor("#D3D3D3");  
  
  Logger.log(boldRows);
  
  //format bolds
  expenseTable.getRow(2).getCell(2).editAsText().setBold(false);
  
  
  for(var i = 2; i < tableData.length; i++)
  {
    expenseTable.getRow(i).getCell(0).editAsText().setBold(false);
    expenseTable.getRow(i).getCell(1).editAsText().setBold(false);
  }
  for(var i = 0; i < boldRows.length; i++)
  {
    expenseTable.getRow(boldRows[i]).getCell(0).editAsText().setBold(true);
  }
  
  //set spacing
  expenseTable.getRow(0).getCell(0).setWidth(300);
  expenseTable.setAttributes(tableStyle);
  
  //WARNING
  var footer = body.appendParagraph("THIS IS AN ESTIMATE ONLY AND FIGURES ARE SUBJECT TO CHANGE");
  footer.setAlignment(DocumentApp.HorizontalAlignment.CENTER);
  footer.editAsText().setBold(true);
  footer.editAsText().setFontSize(7);
}

function formatCurrency(num)
{
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
    {
        num = "0";
    }

    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();

    if (cents < 10)
    {
        cents = "0" + cents;
    }
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
    {
        num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
    }

    return (((sign) ? '' : '-') + '$' + num + '.' + cents);
}

Upvotes: 1

Views: 838

Answers (1)

Tanaike
Tanaike

Reputation: 201358

I believe your goal as follows.

  • You want to reduce the row height of each row in the table of Google Document using Google Apps Script.

Modification points:

  • In order to change the row height of each row in a table, I think that the paddings of top and bottom are required to be modified.
  • From I have tried setting the "minimum height" for each row, but this does not seem to affect my tables., I thought that in this case, the minimum height of the row depends on the font size, and this might be the reason of your issue. For more reduction of the height, the paddings of top and bottom of cell are modified.

Modified script:

In this modification, I prepare one more function for reducing the row height of each row in the table.

function changePaddings() {
  var paddingTop = 0; // You can adjust the height by modifying this.
  var paddingBottom = 0; // You can adjust the height by modifying this.
  var doc = DocumentApp.openByUrl('URL');  // Please set the Google Document URL.

  var tables = doc.getBody().getTables();
  tables.forEach(table => {
    for (var r = 0; r < table.getNumRows(); r++) {
      var row = table.getRow(r);
      for (var c = 0; c < row.getNumCells(); c++) {
        row.getCell(c).setPaddingTop(paddingTop).setPaddingBottom(paddingBottom);
      }
    }
  });
}
  • Please put this after generateDoc() was run like below.

      generateDoc(purchasePrice,commission, hoa, other, recording, escrow, netProceeds);
      changePaddings();
    
  • Or, please put changePaddings(); to the last line of generateDoc().

Result:

Before changePaddings() is not used, the following result is obtained.

enter image description here

After changePaddings() was used, the following result is obtained.

enter image description here

References:

Upvotes: 3

Related Questions