What is the best way to rearrange an HTML table using Google Apps Script?

I'm working with a google sheet that has a script generating email content in the form of an HTML table. Everything is running smoothly except for one issue - the structure of the table in the email is causing the second column to be pushed over due to the length of a link. This creates a large space between the text and the value returned. Is there any way to adjust the table structure so that column 2 stays aligned next to column 1? Perhaps there's an option for alignment or overflow handling? Below is the HTML code for the table.

   

function form1Submit(e) {
      if(e.values && e.values[1] && e.values[2]) {
        var html='<table>';
        //...

After implementing a few changes, here is an updated version of the script that resolved the alignment issue and improved column formatting.

function form1Submit(e) {
  if(e.values && e.values[1] && e.values[2]) {
    var url='https://docs.google.com/spreadsheets/d/e/...';

    // Adjusted styling for the table
    var html='<style>td,th{padding:overflow-wrap: break-word;}table{table-layout:fixed;width:100%;}</style><table>';
    
    // Improved column formatting
    html+=Utilities.formatString('<tr><td>%s</td><td style="width:35%;" colspan ="2"><font size="4" color="red"><b>%s</b></font></td><td style="width:65%;">%s</td></tr>', ' ', '**** DO NOT REPLY TO THIS EMAIL ****', ' ');
    //...

Logger.log(html);
GmailApp.sendEmail(getGlobal('form1Email'), getGlobal('form1Subject'), '', {htmlBody:html});
  }
}

Thank you!

Answer №1

Before I delve into answering your query, take a look at the code snippet provided:

function form1Submit(e) {
      if(e.values && e.values[1] && e.values[2]) {
        var html='<table>';
        html+=Utilities.formatString('<tr><td>%s</td><td><strong>%s</strong></td><td>%s</td></tr>','&nbsp;','Safety Findings Comment:',e.values[7]);
        html+=Utilities.formatString('<tr><td>%s</td><td colspan ="2">%s</td><td>%s</td></tr>','&nbsp;','https://docs.google.com/spreadsheets/d/e/2PACX-1vQj3j6QgrCyvULYo1IeE3q9L9Gzvz2tVvNI8650nhl-L0cQwyx93tRIeuXPxxxxxxxxxxxxxxxxxxx/pubhtml#','&nbsp;' );
        html+='</table>';
    Logger.log(html);
    GmailApp.sendEmail(getGlobal('form1Email'), getGlobal('form1Subject'), '', {htmlBody:html});
  }
}

It's important to note that without clarity on what e.values actually contains, it becomes challenging to test out this code due to undefined variables. Therefore, for reproducibility purposes, I've created my own mock input.

To simulate the email feature, instead of sending emails, I display the outcome in a dialog box. The necessary CSS styling is within the HTML string for potential adaptation in an email scenario.

function form1Submit(e) {
      if(e.values && e.values[1] && e.values[2]) {
        var url='https://docs.google.com/spreadsheets/d/e/2PACX-1vQj3j6QgrCyvULYo1IeE3q9L9Gzvz2tVvNI8650nhl-L0cQwyx93tRIeuXPxxxxxxxxxxxxxxxxxxx/pubhtml#';
        var html='<style>td,th{padding:2px;border:1px solid black;overflow-wrap: break-word;}table{table-layout:fixed;width:100%;}</style><table>';
        html+=Utilities.formatString('<tr><td>&nbsp;</td><td><strong>Safety Findings</strong></td><td>%s</td></tr>',e.values[7]);
        html+=Utilities.formatString('<tr><td>&nbsp;</td><td>%s</td><td>&nbsp;</td></tr>',url);
        html+='</table>';
    Logger.log(html);
    //GmailApp.sendEmail('email', 'Subjecet', '', {htmlBody:html});
    SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html), "Test");    

  }
}

function testAbove() {
  var e={values:['one','two','three','four','five','six','seven','eight','nine']};
  form1Submit(e) 
}

Visual representation of the dialog output can be seen below:

https://i.sstatic.net/Jk4rt.jpg

An alternate approach involves setting individual column widths as depicted below:

function form1Submit(e) {
      if(e.values && e.values[1] && e.values[2]) {
        var url='https://docs.google.com/spreadsheets/d/e/2PACX-1vQj3j6QgrCyvULYo1IeE3q9L9Gzvz2tVvNI8650nhl-L0cQwyx93tRIeuXPxxxxxxxxxxxxxxxxxxx/pubhtml#';
        var html='<style>td,th{padding:2px;border:1px solid black;overflow-wrap: break-word;}table{table-layout:fixed;width:100%;}</style><table>';
        html+=Utilities.formatString('<tr><td style="width:10%;">&nbsp;</td><td style="width:60%;"><strong>Safety Findings</strong></td><td style="width:30%;">%s</td></tr>',e.values[7]);
        html+=Utilities.formatString('<tr><td>&nbsp;</td><td>%s</td><td>&nbsp;</td></tr>',url);
        html+='</table>';
    Logger.log(html);
    //GmailApp.sendEmail('email', 'Subjecet', '', {htmlBody:html});
    SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html), "Test");

  }
}

https://i.sstatic.net/Pf83H.jpg

You have the flexibility to customize this layout by adjusting the width attributes of each column independently.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieve the size and position of the Kendo UI Splitter using the methods getSize()

I am currently working on a webpage using Kendo UI that allows users to customize the layout. I would like to implement a feature where the layout is saved so that the server can later send the user their last saved layout. One of the components I am usin ...

Batch Script for Reading and Writing HTML files

My experience with writing DOS Batch scripts has been limited to basic tasks. However, I recently encountered a need to create a script that reads an HTML template file and generates a new file from it. I successfully managed to store the file data in a va ...

Get the application/pdf document that was just sent to you from the software backend

I am facing an issue with downloading a PDF file sent from the backend. Upon receiving a blob response, I notice that when I download and view the file, the sheets are empty, matching the expected number of sheets. Could this be a coding problem? Current ...

Looking for a way to validate file uploads in WordPress Gravity Forms before final submission

Is there a way to incorporate pre-validation in WordPress gravity form to alert users if they upload a file with the wrong extension, such as something other than PDF? I currently have ajax="true" enabled in the gravity form shortcode to avoid refreshing t ...

Adding a JSON array to HTML using JavaScript

Looking to incorporate JSON Array data into an HTML list with Headings/Subheadings using JavaScript. I experimented with two methods - one involving jQuery and the other mostly vanilla JS. The initial attempt (link here: http://jsfiddle.net/myu3jwcn/6/) b ...

The method of utilizing React with Redux to display component properties

I am currently trying to include my common component in my main.js file Successfully implemented this However, when attempting to print my Redux data values in the common component, I created a method called handleClickForRedux to handle this task. Even af ...

Ensuring Form Validation in Symfony 2.0 View

I am currently using Symfony 2.0 and I have a view file called test.html.php which includes a form: <form action="" method="post" rel=""> <input type="hidden" name="action" value="test" /> <input name="myinput" type="text" value="" ...

Maintain consistent row heights in the Tailwind grid to avoid any row from stretching to match the height of the tallest row

EDIT 2: Here's an example with filler text added: https://play.tailwindcss.com/KPrBMJr6vI At a viewport width of "2XL," you'll notice that the heights of Blocks 3 and 4 are exceptionally tall. Block 5, on the other hand, behaves as desired. My c ...

Understanding the separation and communication techniques in Vue.js components

I recently developed a small vuejs application and I find myself getting confused with the functioning of components. Here is the code snippet that I have been working on: <div id="app"> <div v-if="loggedIn"> <nav> <r ...

Customizing input behavior - Managing the value and onChange event of a controlled <input /> element in VueJS

Switching over from React, I am familiar with the idea of taking "control" of an html element's value and events. Now, I am exploring how to achieve a similar concept in VueJS. Here is what I have so far: <input :value="foo" @input="changeFoo" pa ...

What distinguishes line-height:1.5 from line-height:150% in CSS?

Does anyone have any information on this? ...

Capturing both inner text and inner tags with BeautifulSoup

One of the challenges I'm facing is with a document I am parsing, which contains a mixture of div tags and inline text. Extracting the contents in order has proven to be tricky. Let's consider an example: <div> <div>1</div> &l ...

"Implement a delay in key presses using the setTimeout function to create a pause

I'm having trouble with a function that reloads messages within a div using a timer. I've tried several methods to create a stable timer that runs consistently every 4 seconds, but so far each attempt results in the timer maxing out and running m ...

Getting rid of the Horizontal Scroll Bar

Having trouble with a persistent horizontal scrollbar in the "section3__container" container despite attempts to adjust size and overflow settings. Need assistance in removing this unwanted feature. <html lang="en"> <head> <m ...

I'm experiencing some unusual behavior with the Windows media app when using HTML and CSS

I have a Windows Media Player box on my page, but it overlaps every piece of HTML code. How can I get it to move to the back so that I can still use it? Another problem is that everyone who visits my page needs a plugin to load it, even though most people ...

Can the ::before selector be used in HTML emails?

Hey there, I have a question that might sound silly but I'm having trouble finding an answer online. My issue is with styling the bullet point before a list item in an HTML email. All I want to do is change the bullet point to a square and give it a ...

Horizontal Scrolling Menu for Dynamic Page Navigation

My goal was to create a smooth-scrolling one-page website that scrolls horizontally. One feature I wanted was a menu that smoothly slides into view whenever a new page is in focus along with the horizontal scrolling. I found an example of a scrolling scr ...

Adjusting the size of a parent flexbox to expand or contract in relation to another flexbox

Within my parent div, I have implemented flexbox. Adjacent to the parent div is another flexbox. The flexbox on the right side can contain 1, 2, or 3 items that influence its growth/shrinkage. I expect the size of the parent div to adjust according to th ...

A dynamic 3-column layout featuring a fluid design, with the middle div expanding based on the

Sorry for the vague title, I'm struggling to explain my issue clearly, so let me elaborate. I am using flexbox to create a 3-column layout and want the middle column to expand when either or both of the side panels are collapsed. Here is a screenshot ...

Get rid of unnecessary markup rather than just hiding it in ASP.NET

Is it possible to completely remove unused controls from the codebehind when designing a page in ASP.NET instead of just hiding them? Hiding controls can result in whitespace on user screens. For example: <td> <uc1:GoToRequestControl id="UserCon ...