Google Apps Script实现Docs表格行禁止跨页溢出及索引问题
问题:Google Docs表格行禁止跨页显示的实现问题
我在Sheets中开发了一个侧边栏,用于在Docs、Sheets和Slides中创建带图片的表格并添加批注。目前Docs环节已实现创建文档、导入图片到表格、设置列数,但卡在禁止表格行跨页显示的功能上,现有方案无法适配自身代码,需要针对性解决。
现有可运行代码
// 1. 创建新的Docs文件 dossierSauvegarde = DriveApp.getFolderById(tReponse[1]); document = DocumentApp.create(tReponse[2]); DriveApp.getFileById(document.getId()).moveTo(dossierSauvegarde); // 2. 获取文件夹中图片的名称和ID body = document.getBody(); dossierPhotos = DriveApp.getFolderById(tReponse[0]); fichiers = dossierPhotos.getFiles(); while (fichiers.hasNext()) { fichier = fichiers.next(); files.push({name: fichier.getName(), id: fichier.getId()}); } // 3. 创建表格 body.insertTable(0); tableau = body.getTables(); // 4. 按名称排序图片 files = files.sort(function(a, b){ nomA = a.name.toUpperCase(); nomB = b.name.toUpperCase(); return nomA.localeCompare(nomB); }); // 5. 添加包含图片的行 tableau.forEach(table => { files.forEach(function(file){ // 5.1. 获取图片ID idFile = file.id; img = DriveApp.getFileById(idFile); // 5.2. 添加新行 tr = table.appendTableRow(); // 5.3. 添加图片 photo = tr.appendTableCell().appendImage(img); // 5.4. 调整过大图片的尺寸 width = photo.getWidth(); height = photo.getHeight(); ratio = width / height; if (photo.getWidth() > 250){ photo.setWidth(250); photo.setHeight(parseInt(250/ratio)); } for (let i = 0; i < parseInt(tReponse[4]) - 1; i ++){ tr.appendTableCell(""); } // 5.5. 添加样式(将图片在单元格中水平居中) styles[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER; photo.getParent().setAttributes(styles); // 5.6. 递增变量"nbPhotosDocs" nbPhotosDocs ++; }); }); // 6. 保存执行记录 fSauvegarde.appendRow([Utilities.formatDate(new Date(),"GMT+2","dd/MM/yyyy"),mailUtilisateur,tReponse[3],tReponse[2],tReponse[4],nbPhotosDocs,document.getUrl()]); // 7. 显示文件链接 htmlOutput = HtmlService.createHtmlOutput('<p>生成文件链接:<a href="' + document.getUrl() + '" target="_blank">链接</a></p>') .setWidth(300) .setHeight(80); SpreadsheetApp.getUi().showModalDialog(htmlOutput, '表格已生成'); document.saveAndClose();
核心问题
上述代码能正常生成表格,但无法获取表格行(tr)的索引,因此无法应用以下禁止跨页的代码片段:
const table = body.getTables()[0]; // 请设置表格的索引 const index = Docs.Documents.get(documentId).body.content[body.getChildIndex(table) + 1].startIndex; const requests = [{ updateTableRowStyle: { tableRowStyle: { preventOverflow: true }, tableStartLocation: { index }, fields: "preventOverflow", } }]; Docs.Documents.batchUpdate({ requests }, documentId);
解决方案
步骤1:启用Google Docs API
在Google Apps Script编辑器中,依次点击资源 > 高级Google服务,找到并开启Google Docs API。
步骤2:添加禁止跨页的代码
在document.saveAndClose();之前插入以下代码,即可实现表格所有行禁止跨页:
// 获取文档ID和目标表格 const documentId = document.getId(); const targetTable = body.getTables()[0]; // 通过Docs API获取文档结构,定位表格的起始索引 const docStructure = Docs.Documents.get(documentId); const tableContentPosition = body.getChildIndex(targetTable) + 1; const tableStartIndex = docStructure.body.content[tableContentPosition].startIndex; // 构建批量更新请求 const updateRequests = [{ updateTableRowStyle: { tableRowStyle: { preventOverflow: true }, tableStartLocation: { index: tableStartIndex }, fields: "preventOverflow" } }]; // 执行批量更新 Docs.Documents.batchUpdate({ requests: updateRequests }, documentId);
解释
这段代码通过Docs API获取文档的底层结构,精准定位到表格的起始位置,然后批量将该表格的所有行设置为preventOverflow: true,从而禁止行跨页显示。无需单独获取每一行的索引,一次请求即可作用于表格所有行。
内容的提问来源于stack exchange,提问作者DAM
相关产品推荐
相关产品推荐

