セル内にテキストがある場合にテーブルセルの正しい高さを取得する方法

Aug 17 2020

履歴: -図形のサイズがテーブルのセルサイズと等しくなく、図形内にテキストを収める

ソリューションは完璧に働いていたが、私は「で行ったように、我々は、任意のテキストのテキストの高さを変更しますいくつかの問題が見つかったテキスト1」、私は「の高さが変更され、テキスト1を、行の高さの結果が0.0になり、」

Google Apps Scriptの制限:セルの高さを取得できず、セルのパディングを取得できません。

いくつかの調査の後、セルの段落をテキストとして抽出しようとしました。

私と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

それでも問題は、形状の高さがテーブルセルと等しい場合と同じです。画像を参照してください

回答

1 Tanaike Aug 17 2020 at 23:57

この変更はどうですか?

変更点:

  • 確認してみる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スライドを上記の変更されたスクリプトで使用すると、次のようになります。

注意:

  • これは、サンプル用の単純なサンプルスクリプトです。そのため、他のサンプルを使用する場合は、スクリプトの変更が必要になる場合があります。これに注意してください。
  • この場合、サンプルから、各段落が同じフォントサイズであると想定しています。これに注意してください。