r/GoogleAppsScript • • Sep 26 '25

Question Exception: Argument cannot be null: textAlignment

I'm creating a PDF from a Google Sheet with this code and trying to make the quantity line up as a number and add a Link field that says "Link" with a hyperlink that the customer can click. The PDF is an invoice built from the Sheet. I've tried a bunch of things to solve this error but am not sure what to do. This is the code causing the problem.

for (let i = 0; i < plants.length; i++) {
  const plant = plants[i];
  const row = table.appendTableRow();
  
  // Create the Quantity cell and apply right alignment
  const quantityCell = row.appendTableCell();
  quantityCell.setWidth(widths[0]);
  // Always create a paragraph in the cell
  const quantityParagraph = quantityCell.appendParagraph(plant.quantity ? plant.quantity.toString() : "");
  // Now safely set the alignment on the paragraph
  quantityParagraph.setTextAlignment(DocumentApp.TextAlignment.RIGHT);
     
  // Create other cells with default alignment
  row.appendTableCell(plant.size ? plant.size.toString() : "").setWidth(widths[1]);
  row.appendTableCell(plant.latinName ? plant.latinName.toString() : "").setWidth(widths[2]);
  row.appendTableCell(plant.variety ? plant.variety.toString() : "").setWidth(widths[3]); 
  row.appendTableCell(plant.commonName ? plant.commonName.toString() : "").setWidth(widths[4]);
  row.appendTableCell(plant.code ? plant.code.toString() : "").setWidth(widths[5]);
  row.appendTableCell(plant.plantnotes ? plant.plantnotes.toString() : "").setWidth(widths[6]);
  
  // Create the Link cell
  const linkCell = row.appendTableCell();
  linkCell.setWidth(widths[7]);
  
  // If a link exists, add the clickable text and center it.
  if (plant.link) {
    const linkParagraph = linkCell.appendParagraph("Link");
    linkParagraph.setLinkUrl(plant.link);
    linkParagraph.setTextAlignment(DocumentApp.TextAlignment.CENTER);
  }
}
1 Upvotes

4 comments sorted by

View all comments

Show parent comment

2

u/biztechninja Sep 28 '25

An AI code assistant. I'm still learning appscript and have been using AI to help me with this project. Planning to take courses on Udemy to actually learn how to do it myself.