كيفية حذف صفوف فارغة من الجداول في مستندات Google الخاصة بك

تساعدك إضافة المستندات الإضافية في استوديو المستندات في إنشاء مستندات Google من البيانات في أوراق Google وردود Google Form. يمكنك إنشاء قالب في مستندات Google وسيحل الوظيفة الإضافية محل العناصر النائبة بالإجابات المقدمة في استجابة نموذج Google.
ومع ذلك ، قد يخلق هذا النهج الكثير من الصفوف الفارغة في الجدول للحصول على إجابات ليس لها استجابة في نماذج Google. لإعطائك مثالًا ، إذا لم يرد المستخدم على Age
سؤال ، سيكون للوثيقة التي تم إنشاؤها صف لـ {{Age}}
سؤال ولكن مع قيمة فارغة.
قم بإزالة الصفوف الفارغة في مستندات Google
بمساعدة البرنامج النصي لتطبيقات Google ، يمكننا بسهولة سحب جميع الجداول الموجودة في نص مستند Google ، وتكرارها عبر كل صف في الجدول ، وإذا لم تكن هناك قيمة في الصف ، فيمكننا إزالة الصف بأمان من طاولة.
داخل مستند Google الخاص بك ، انتقل إلى قائمة الأدوات ، واختر محرر البرنامج النصي ولصق الرمز التالي. انتقل إلى قائمة Run واختر RemoveBlankrows من القائمة المنسدلة لتشغيل البرنامج النصي.
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();
};
حذف صفوف الجدول الفارغة في شرائح Google
يمكنك استخدام نفس التقنية لإزالة الصفوف الفارغة من الجداول الموجودة في عرض Slide Google الخاص بك.
إذا كان جدول شرائح Google الخاص بك يستخدم الخلايا المدمجة ، فقد ترغب في التحقق من حالة دمج خلية مع SlidesApp.CellMergeState.MERGED
التعداد.
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();
};