Hey, friends today I will teach you How to Delete Blank Rows from Tables in your Google Documents. so let get started with today Code snippets. Getting different problems is altogether gives a very different experience. today the Code snippets I am going to share with you is How to Delete Blank Rows from Tables in your Google Documents.
The Document Studio add-on creates Google Docs from data in Google Sheets and Google Form responses. You can create a template in Google Docs, and the add-on will replace the placeholders with responses from the Google Form.
This method, however, may result in a large number of blank rows in the table for answers that have no response in Google Forms. To illustrate, if the user has not responded to the Age question, the generated document will include a row for the Age question but with a blank value.
You might also like our trending code snippets
- How to Request Payments with Stripe Checkout and Google Sheets
- How to Improve Performance of Google Apps Script with Memoization
- How to Make all Shapes the Same Size in Google Slides
- Best way to Import Lodash in your JavaScript Projects for Lowest Bundle Size
Remove Blank Rows in Google Docs
We can easily pull all tables contained in the body of a Google Document using Google Apps Script, iterate through each row in the table, and safely remove the row from the table if there is no value in the row.
Go to the Tools menu in your Google Document, select Script Editor, and paste the following code. To run the script, go to the Run menu and select RemoveBlankRows from the dropdown menu.
const removeBlankRows = () => {
// Replace all whitespaces and check if the cell is blank
const isBlankCell = (text = "") => !text.replace(/\s/g, "");
// Does the row have any data other than in column 1 (header)
const rowContainsData = (row) => {
const columnCount = row.getNumCells();
let rowHasFilledCell = false;
for (
let columnIndex = 1;
columnIndex < columnCount && !rowHasFilledCell;
columnIndex += 1
) {
const cellValue = row.getCell(columnIndex).getText();
if (!isBlankCell(cellValue)) {
rowHasFilledCell = true;
}
}
return rowHasFilledCell;
};
// Get the current document
const document = DocumentApp.getActiveDocument();
document
.getBody()
.getTables()
.forEach((table) => {
const rowCount = table.getNumRows();
for (let rowIndex = rowCount - 1; rowIndex >= 0; rowIndex -= 1) {
const row = table.getRow(rowIndex);
if (isBlankCell(row.getText()) || !rowContainsData(row)) {
// Remove the row from the Google Docs table
table.removeRow(rowIndex);
}
}
});
// Flush and apply the changes
document.saveAndClose();
};
Delete Blank Table Rows in Google Slides
You can use the same method to remove blank rows from tables in your Google Slide presentation.
If your Google Slides table uses merged cells, you may want to check merge status of a cell with the SlidesApp.CellMergeState.MERGED enum.
const removeBlankRows = () => {
// Get the current document
const presentation = SlidesApp.getActivePresentation();
presentation.getSlides().forEach((slide) => {
slide.getTables().forEach((table) => {
const rowCount = table.getNumRows();
for (let rowIndex = rowCount - 1; rowIndex >= 0; rowIndex -= 1) {
const row = table.getRow(rowIndex);
const cellCount = row.getNumCells();
let rowHasFilledCell = false;
for (
let cellIndex = 1;
cellIndex < cellCount && !rowHasFilledCell;
cellIndex += 1
) {
const cellValue = row.getCell(cellIndex).getText().asString();
if (cellValue.trim() !== "") {
rowHasFilledCell = true;
}
}
if (!rowHasFilledCell) {
row.remove();
}
}
});
});
// Flush and apply the changes
presentation.saveAndClose();
};
Please comment if you like this article thankyou

