繁体   English   中英

当单元格内的文本时如何获得表格单元格的正确高度

[英]How to get the correct height of Table cell when text inside the cell

历史: - 形状大小不等于表格单元格大小并且适合形状内的文本

解决方案工作得很好,但是当我们改变任何文本的文本高度时我发现了一些问题就像我对“ Text 1 ”所做的那样,我改变了“ Text 1 ”的高度,那么行高的结果将为 0.0

在此处输入图像描述

Google Apps 脚本的限制:我们无法获取单元格的高度,无法获取单元格的内边距。

经过一些研究尝试将单元格的段落提取为文本。

用我和Tanaike修改更新了脚本:

var rowHeight = 0.0;

  var tableCell = pageElements[index].asTable().getCell(ir, ic);

  //Get cell color
  let cellFill = tableCell.getFill().getSolidFill();

  var cellParaText = tableCell.getText().getParagraphs();
  cellParaText.forEach(function(item, index) {
    
    var t = cellParaText[index].getRange();

    //I tried to applied Tanaike's solution here

    rowHeight = rowHeight + ( t.asString().split("\n").length - 1 ) * ( t.getTextStyle().getFontSize() / 72 * 96 )

    //rowHeight = rowHeight + cellParaText[index].getRange().getTextStyle().getFontSize();

    rowHeight = rowHeight + cellParaText[index].getRange().getParagraphStyle().getSpaceAbove()
    rowHeight = rowHeight + cellParaText[index].getRange().getParagraphStyle().getSpaceBelow()
    rowHeight = rowHeight + (cellParaText[index].getRange().getParagraphStyle().getLineSpacing()/100)

  });


  //Get cell text, text color and text background color
  let cellText = tableCell.getText().asString()

  //insert the RECTANGLE shape 
  let insertedShape = selection.getCurrentPage()
                        .insertShape(SlidesApp.ShapeType.RECTANGLE, cellLeft+5, cellTop+5, columnWidth, rowHeight);

完整的脚本在这里https://pastebin.com/keXKHbhC

这是我在 GSlide 中的表格 - https://docs.google.com/presentation/d/10nupvk-2BZDSV6-gPn_n2LkrUtkCxot7qr_jcZGBHqw/edit#slide=id.p

形状高度等于表格单元格的问题仍然相同,参见图片

在此处输入图像描述

这个改装怎么样?

修改点:

  • 当我检查getSpaceAbove()getSpaceBelow()时,它们似乎是0
  • 我认为可能会使用getLineSpacing()
  • 而且,我注意到了更重要的一点。 例如,当有 3 个段落时,似乎需要使用 4 个段落的空格。

当这一点反映到您的https://pastebin.com/keXKHbhC脚本中时,它变为如下。

修改后的脚本:

从:

cellParaText.forEach(function(item, index) {

  var t = cellParaText[index].getRange();
  
  //i tried to applied Tanike's solution here
  rowHeight = rowHeight + ( t.asString().split("\n").length - 1 ) * ( t.getTextStyle().getFontSize() / 72 * 96 )
  
  //rowHeight = rowHeight + cellParaText[index].getRange().getTextStyle().getFontSize();
  
  rowHeight = rowHeight + cellParaText[index].getRange().getParagraphStyle().getSpaceAbove()
  rowHeight = rowHeight + cellParaText[index].getRange().getParagraphStyle().getSpaceBelow()
  rowHeight = rowHeight + (cellParaText[index].getRange().getParagraphStyle().getLineSpacing()/100)

});

至:

cellParaText.forEach(function(item, index, a) {
  var t = cellParaText[index].getRange();
  var lineSpace = cellParaText[index].getRange().getParagraphStyle().getLineSpacing() / 100;
  var fontSize = t.getTextStyle().getFontSize() / 72 * 96;
  rowHeight += fontSize * (a.length > 1 ? lineSpace : 1);
  if (index == a.length - 1) rowHeight += fontSize;
});

另外,请进行如下修改。

从:

insertedShape.getText()
            .getParagraphStyle()
            .setLineSpacing(cellParagraph.getLineSpacing()/100)
            .setSpaceAbove(cellParagraph.getSpaceAbove())
            .setSpaceBelow(cellParagraph.getSpaceBelow())
            .setSpacingMode(cellParagraph.getSpacingMode())
            .setParagraphAlignment(cellParagraph.getParagraphAlignment())

至:

insertedShape.getText()
            .getParagraphStyle()
            .setParagraphAlignment(cellParagraph.getParagraphAlignment())

结果:

当您的示例 Google 幻灯片与上述修改后的脚本一起使用时,它变为如下。

在此处输入图像描述

笔记:

  • 这是您的示例的简单示例脚本。 因此,当您使用其他示例时,可能需要修改脚本。 请注意这一点。
  • 在这种情况下,从您的示例中,它假设每个段落的字体大小相同。 请注意这一点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM