当前位置:首页 > 未分类 > 正文内容

How to Automatically Format Google Form Responses in Google Sheets

ceacer3周前 (05-02)未分类6313

When someone submits a new Google Form response, the form answers are automaticaly added as a new row in the Google Sheet that is linked to the form. The only problem here is that Google Forms will not add any formatting or styles to the new row that you may have applied to the previous rows of the sheet.

Let me illustrate this with an example.

Here’s a screenshot of a Google Sheet that is storing Google Form responses. I’ve changed the default font family to Droid Sans, center-aligned the Country and Age column and also applied a different date format to the Date of Birth column.

Google Forms Response Sheet

The formatting looks good but as soon as a new Google Form submissions is received, the new response row appended to the Google Sheet will lose all the formatting applied to the previous rows.

As you can see in the screenshot below, the cell alignment is not preserved, the custom date formats are ignored and so is the default font size and font family.

Google Forms Auto Formatting

Auto Format New Rows in Google Sheets

Since there’s no way for us to override this Google Forms behavior, we can take the help of Google Apps Script to automatically format new rows in Google Sheets that are added through Google Forms.

To get started, open the Google Sheet and format the last row with the styles that you would like to apply to incoming form responses. Please ensure that there is at least one form response in the Google Sheet where you can apply the desired formatting that you want to be applied to new rows.

Add Google Apps Script to Google Sheet

Next, go to Extensions > Apps Script menu inside Google Sheets and copy-paste the Google Script below.

/**
 * @OnlyCurrentDoc
 */

const formatRowOnFormSubmit = formEvent => {
  try {
    const { range } = formEvent || {};
    if (!range) throw new Error("This function should only be triggered by form submissions");

    const sheet = range.getSheet();
    const currentRow = range.getRowIndex();
    const endColumn = sheet.getLastColumn();

    // Skip formatting if this is the first or second row
    if (currentRow <= 2) return;

    // Copy formatting from previous row to new row
    const sourceRange = sheet.getRange(currentRow - 1, 1, 1, endColumn);
    sourceRange.copyFormatToRange(sheet, 1, endColumn, currentRow, currentRow);
  } catch (error) {
    console.error(`Error formatting new response: ${error.message}`);
  }
};

Save the script. Next, we’ll create an onFormSubmit trigger inside the Google Sheet that will execute the formatRowOnFormSubmit function whenever a new form is submitted. This trigger will take whatever formatting that has been applied to the previous row and apply that to the current row.

To create the trigger, go to the Triggers section in the sidebar and click + Add Trigger. Under the Event type dropdown, select On form submit and save the trigger. That’s it!

Google Forms Trigger

A previous version of the script used the copyTo method to copy formatting. While this approach works, the current copyFormatToRange method is more efficient as it’s specifically designed for copying only formatting between ranges.

const targetRange = sheet.getRange(currentRow, 1, 1, endColumn);
sourceRange.copyTo(targetRange, SpreadsheetApp.CopyPasteType.PASTE_FORMAT);

Conditional Formatting in Google Sheets

Learn more about conditional formatting in Google Sheets that allows you to apply automatic formatting to cells in spreadsheets that meet certain criteria.

Also see: Automate Google Forms through Workflows

相关文章

How to Use Conditional Logic in Google Documents

How to Use Conditional Logic in Google Documents

Conditional content allows you to customize your Google Docs template and generate different version...

Convert Google Docs and Google Sheets with Apps Script

You can easily convert any Google Spreadsheet or Google Document in your Google Drive to other forma...

How to Make Personalized Place Cards with Guest Names

How to Make Personalized Place Cards with Guest Names

Whether it is a wedding party or a business conference, those tent-shaped place cards are ideal for...

How to Play an MP3 File in Google Sheets

How to Play an MP3 File in Google Sheets

You can put the link of any MP3 audio file in Google Sheets but when you click the file link, the au...

How to Auto

How to Auto

This tutorial describes how you can use Google Sheets to build your own podcast manager. You can spe...

How to Use Conditional Formatting in Google Sheets to Highlight Information

How to Use Conditional Formatting in Google Sheets to Highlight Information

Conditional formatting in Google Sheets makes it easy for you to highlight specific cells that meet...