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

تساعدك الوظيفة الإضافية Document Studio على إنشاء مستندات Google من البيانات الموجودة في جداول بيانات Google واستجابات نماذج Google. يمكنك إنشاء قالب في محرر مستندات Google وستقوم الوظيفة الإضافية باستبدال العناصر النائبة بالإجابات المقدمة في استجابة نموذج Google.
ومع ذلك، قد يؤدي هذا الأسلوب إلى إنشاء الكثير من الصفوف الفارغة في الجدول للإجابات التي ليس لها استجابة في نماذج Google. لأعطيك مثالا، إذا لم يقم المستخدم بالرد على Age
السؤال، سوف يحتوي المستند الذي تم إنشاؤه على صف لـ {{Age}}
سؤال ولكن بقيمة فارغة.
قم بإزالة الصفوف الفارغة في مستندات Google
بمساعدة Google Apps Script، يمكننا بسهولة سحب جميع الجداول الموجودة في نص مستند Google، والتكرار خلال كل صف في الجدول، وإذا لم تكن هناك قيمة في الصف، فيمكننا إزالة الصف بأمان من الجدول. طاولة.
داخل مستند Google الخاص بك، انتقل إلى قائمة “الأدوات”، واختر “محرر البرامج النصية” والصق الكود التالي. انتقل إلى قائمة “تشغيل” واختر 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 يستخدم خلايا مدمجة، فقد تحتاج إلى التحقق من حالة دمج الخلية التي تحتوي على 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();
};