单元格中的文本时如何获取表格单元格的正确高度

时间:2020-08-17 10:50:15

标签: javascript google-apps-script shapes google-slides

历史记录:-Shape size not equal to the table cell size and fit the text inside the shape

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

enter image description here

对Google Apps脚本的限制: 我们无法获得单元格的高度,无法获得单元格的填充。

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

使用我和Tanaike修改的更新脚本:

#include "MainWindow.h"
#include "ui_MainWindow.h"
#include<QOpenGLWidget>

QOpenGLWidget *openglWidget;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    openglWidget = new QOpenGLWidget;
    openglWidget->setAttribute(Qt::WA_DeleteOnClose);
    ui->gridLayout->addWidget(openglWidget);
}

void MainWindow::on_pushButton_2_clicked()
{
    ui->gridLayout->removeWidget(openglWidget);
    openglWidget->close();
    delete openglWidget;
}

完整脚本,https://pastebin.com/keXKHbhC

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

仍然存在问题,形状高度等于表格单元格,请参见图片

enter image description here

1 个答案:

答案 0 :(得分:1)

该修改如何?

修改点:

  • 当我选中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幻灯片与以上修改的脚本一起使用时,它如下所示。

enter image description here

注意:

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