关闭跨页表格行溢出

问题描述 投票:0回答:1

我正在表格中开发一个侧边栏,以便我可以创建照片表格(在文档、表格和幻灯片中)并对其发表评论。 但我被困在文档中。我可以生成一个文档,将照片导入表格并设置列数。 但是,我想禁用页面之间表格的行溢出,但我不知道如何操作。我发现article提到了我的问题,但我无法将其应用到我的开发中。我希望我的问题不会被视为重复。

这是我的代码

      // 1. Création du nouveau fichier Docs
      dossierSauvegarde = DriveApp.getFolderById(tReponse[1]);
      document = DocumentApp.create(tReponse[2]);
      DriveApp.getFileById(document.getId()).moveTo(dossierSauvegarde);

      // 2. Récupération des noms et des id des photos présentes dans le dossier
      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. Création du tableau
      body.insertTable(0);
      tableau = body.getTables();

      // 4. Tri des photos par nom
      files = files.sort(function(a, b){
        nomA = a.name.toUpperCase();
        nomB = b.name.toUpperCase();
        return nomA.localeCompare(nomB);
      });

      // 5. Ajout des lignes avec les photos
      tableau.forEach(table => {
        files.forEach(function(file){
          // 5.1. Récupération de l'id de la photo
          idFile = file.id;
          img = DriveApp.getFileById(idFile);

          // 5.2. Ajout d'une nouvelle ligne
          tr = table.appendTableRow();

          // 5.3. Ajout de la photo
          photo = tr.appendTableCell().appendImage(img);

          // 5.4. Redimensionnement des photos trop grandes
          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. Ajout du style (centrer horizontalement la photo dans la cellule)
          styles[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER;
          photo.getParent().setAttributes(styles);

          // 5.6. Incrémentation de la variable "nbPhotosDocs"
          nbPhotosDocs ++;
        });
      });

      // 6. Sauvegarde de l'exécution
      fSauvegarde.appendRow([Utilities.formatDate(new Date(),"GMT+2","dd/MM/yyyy"),mailUtilisateur,tReponse[3],tReponse[2],tReponse[4],nbPhotosDocs,document.getUrl()]);

      // 7. Affichage du lien du fichier
      htmlOutput = HtmlService.createHtmlOutput('<p>Lien vers le fichier généré : <a href="' + document.getUrl() + '" target="_blank">Lien</a></p>')
        .setWidth(300)
        .setHeight(80);
      SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Tableau généré');


      document.saveAndClose();

工作完美,但问题是我无法获取 tr 的索引来应用上面的代码。

const table = body.getTables()[0]; // Please set the index of the table.
  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);
google-apps-script google-docs
1个回答
0
投票

我相信您的目标如下。

  • 您想将我的答案用于您的展示脚本。
  • 当我看到你的显示脚本时,似乎脚本不完整,因为没有
    fSauvegarde, tReponse, nbPhotosDocs, files, mailUtilisateur, styles
    的变量。但是,你说
    It's work perfectly
    。所以,我相信你的实际脚本运行良好。

既然如此,下面的修改如何?在使用此脚本之前,请在高级 Google 服务中启用 Google Docs API

来自:

});

// 6. Sauvegarde de l'exécution
fSauvegarde.appendRow([Utilities.formatDate(new Date(),"GMT+2","dd/MM/yyyy"),mailUtilisateur,tReponse[3],tReponse[2],tReponse[4],nbPhotosDocs,document.getUrl()]);

致:

});

document = setPreventOverflow(document); // Added

// 6. Sauvegarde de l'exécution
fSauvegarde.appendRow([Utilities.formatDate(new Date(),"GMT+2","dd/MM/yyyy"),mailUtilisateur,tReponse[3],tReponse[2],tReponse[4],nbPhotosDocs,document.getUrl()]);

并且,请添加以下功能。

function setPreventOverflow(doc) {
  const documentId = doc.getId();
  doc.saveAndClose();
  doc = DocumentApp.openById(documentId);
  const body = doc.getBody();
  const tables = body.getTables();
  const obj = Docs.Documents.get(documentId).body.content;
  const requests = tables.map(table => ({
    updateTableRowStyle: {
      tableRowStyle: { preventOverflow: true },
      tableStartLocation: { index: obj[body.getChildIndex(table) + 1].startIndex },
      fields: "preventOverflow",
    }
  }));
  Docs.Documents.batchUpdate({ requests }, documentId);
  return doc;
}

注:

  • 当您的实际脚本运行良好并且此修改反映在脚本中时,所创建文档的每个表格的“允许行跨页溢出”复选框将被取消选中。

参考:

© www.soinside.com 2019 - 2024. All rights reserved.