Come ottenere l'altezza corretta della cella della tabella quando il testo all'interno della cella

Aug 17 2020

Storia: - La dimensione della forma non è uguale alla dimensione della cella della tabella e adatta il testo all'interno della forma

La soluzione funzionava perfettamente, ma ho riscontrato alcuni problemi quando cambiavamo l'altezza del testo di qualsiasi testo Come ho fatto con " Testo 1 ", ho cambiato l'altezza di " Testo 1 ", quindi il risultato dell'altezza della riga sarà 0,0

Limitazione di Google Apps Script: non possiamo ottenere l'altezza della cella, non possiamo ottenere il riempimento della cella.

Dopo alcune ricerche ho provato a estrarre il paragrafo della cella come testo.

Script aggiornato con la mia modifica e quella di 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);

Script completo qui https://pastebin.com/keXKHbhC

Ecco il mio tavolo in GSlide - https://docs.google.com/presentation/d/10nupvk-2BZDSV6-gPn_n2LkrUtkCxot7qr_jcZGBHqw/edit#slide=id.p

Il problema è ancora lo stesso con l'altezza della forma uguale alla cella della tabella, vedere l'immagine

Risposte

1 Tanaike Aug 17 2020 at 23:57

Che ne dici di questa modifica?

Punti di modifica:

  • Quando ho controllato getSpaceAbove()e getSpaceBelow(), sembra che quelli siano 0.
  • Penso che getLineSpacing()potrebbe essere usato.
  • E ho notato un altro punto importante. Ad esempio, quando ci sono 3 paragrafi, sembra che sia necessario utilizzare gli spazi di 4 paragrafi.

Quando questi punti si riflettono nel tuo script di https://pastebin.com/keXKHbhC, diventa il seguente.

Script modificato:

A partire dal:

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)

});

Per:

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;
});

Inoltre, modificare come segue.

A partire dal:

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

Per:

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

Risultato:

Quando il tuo esempio di Presentazioni Google viene utilizzato con lo script modificato sopra, diventa il seguente.

Nota:

  • Questo è un semplice script di esempio per il tuo campione. Pertanto, quando si utilizza un altro esempio, potrebbe essere necessario modificare lo script. Per favore, stai attento.
  • In questo caso, dal tuo esempio, si suppone che ogni paragrafo abbia la stessa dimensione del carattere. Per favore, stai attento.