形状大小不等于表格单元格大小,并且使文本适合形状 [英] Shape size not equal to the table cell size and fit the text inside the shape

查看:106
本文介绍了形状大小不等于表格单元格大小,并且使文本适合形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图插入一个等于表格单元格大小的形状.

Trying to insert a shape equal to the table cell size.

我使用InsertShape()创建了形状,并将行的高度(可能是单元格的高度),列的宽度(可能是单元格的宽度)复制到了形状.

I created the shape using InsertShape() and copied the height of row (possibly height of cell), width of column (possibly width of cell) to the shape.

问题:

  1. 形状的大小与单元格的高度和宽度不相等.
  2. 形状内的文本已变形,形状的文本应自动适合,而单元格的文本应适合单元内.

我不能说.getRow().getMinimumHeight()的工作方式.

文本长度不固定.可能会是一段很长的段落.

Text length not fixed. It could be like a long paragraph.

Google Apps脚本的局限性:

Limitation of Google Apps Script :

表格单元格-Google Apps脚本无法获取每个单元格的宽度和高度

Table Cell - Google Apps Script can't get the width and height of each cell

Google Apps脚本:

Google Apps Script:

let activePresentation = SlidesApp.getActivePresentation()
let selection = activePresentation.getSelection()
let pageElements = selection.getPageElementRange().getPageElements()

//get table Left, table Top of each table under the pageElement object
var tableLeft = pageElements[0].asTable().getLeft(),
    tableTop = pageElements[0].asTable().getTop();

var cellTop = tableTop;       //cell top possible equal to table top   
var cellLeft = tableLeft;    //cell top possible equal to table Left 

//get the row height and column width
var rowHeight = pageElements[0].asTable().getRow(0).getMinimumHeight();
var columnWidth = pageElements[0].asTable().getColumn(0).getWidth();

//get the internal text of cell
var cellText = pageElements[0].asTable().getCell(0, 0).getText().asString()

//insert shape equal to the table cell size
var shape_t = selection.getCurrentPage()
                   .insertShape(SlidesApp.ShapeType.RECTANGLE, 
                                         cellLeft+200, 
                                         cellTop, 
                                         columnWidth, 
                                         rowHeight);

//set the cell text insde the Shape 
shape_t.getText().setText(cellText);

在这里您可以查看演示 https://docs.google.com/presentation/d/10nupvk-2BZDSV6-gPn_n2LkrUtkCxot7qr_jcZGBGBHqw/edit?usp=sharing

Here you can see the demo https://docs.google.com/presentation/d/10nupvk-2BZDSV6-gPn_n2LkrUtkCxot7qr_jcZGBHqw/edit?usp=sharing

完整脚本: https://pastebin.com/CrV3qHwG

复制问题 在Google脚本编辑器中粘贴上述 https://pastebin.com/CrV3qHwG 脚本, 添加一个具有1行1列的表,显然它将只有一个单元格. 选择表,然后通过单击创建形状"来运行脚本.来自"Test So"菜单.

Replicate Issue Paste above mentioned https://pastebin.com/CrV3qHwG script in Google Script Editor, Add a table with 1 row and 1 column, obviously it will be only one cell. Select the table and then run the script by clicking on "Create Shape" from the "Test So" menu.

图像以快速查看方式显示问题和预期结果

推荐答案

从您的其他信息中,我可以确认您的情况.当我看到它时,我注意到表有一个单元格,并且表大小是默认大小.在这种情况下,表格使用单元格的默认行高.单元格的高度由输入的文本自动设置.这样,行高仅为一行,即381000 EMU(30像素)的高度.关于这种情况,我确认从Slides服务和Slides API都获得了相同的结果.因此,这似乎是当前的规范.

From your additional information, I could confirm your situation. When I saw it, I could notice that the table has one cell, and the table size is the default size. In this case, the table uses the default row height of the cell. And the height of cell is automatically set by the inputted texts. By this, the row height is only one row which is the height of 381000 EMU (30 pixels). About this situation, I confirmed that the same results were obtained from both Slides service and Slides API. So it seems that this is the current specification.

在当前阶段要检索实际高度时,我认为需要通过手动或脚本设置高度并检索高度.这样,表格就是实际的行高.

When you want to retrieve the actual height, in the current stage, I think that it is required to set the height by manual or script and retrieve the height. By this, the table is the actual row height.

或者,尽管这与默认表的行高(包括文本)并不完全相同,但我认为行高也可以使用换行符和字体大小来计算.在此答案中,我将通过修改脚本来介绍此示例脚本.

Or, although this is not the completely same with the row height of the default table including the texts, I think that the row height can be also calculated using the line breaks and the font size. In this answer, I introduce the sample script for this by modifying your script.

为了给出高度,作为一个示例修改,我想提出以下脚本.修改脚本后,它如下所示.

In order to give the height, as a sample modification, I would like to propose the following script. When your script is modified, it becomes as follows.

var rowHeight = pageElements[0].asTable().getRow(0).getMinimumHeight();

到:

var t = pageElements[0].asTable().getRow(0).getCell(0).getText();
var rowHeight = (t.asString().split("\n").length - 1) * (t.getTextStyle().getFontSize() / 72 * 96);

  • 在此修改中,使用换行符和字体大小来计算行高.
    • t.asString().split("\n").length - 1是换行符的数量.
    • t.getTextStyle().getFontSize() / 72 * 96是字体大小的像素值.似乎在Google幻灯片中使用了96 DPI.
    • 但是,不幸的是,在当前阶段,结果与默认表行高度并不完全相同.
    • 在这种情况下,当rowHeight乘以1.08时,将获得相同的高度.但这是没有根据的.请注意这一点.
      • In this modification, the row height is calculated using the line breaks and the font size.
        • t.asString().split("\n").length - 1 is the number of line breaks.
        • t.getTextStyle().getFontSize() / 72 * 96 is the pixel value of the font size. It seems that in Google Slides, 96 DPI is used.
        • But, unfortunately, in the current stage, the result is not the completely same with the default table row height.
        • In this case, when rowHeight is multiplied by 1.08, the same height is obtained. But this factor is unfounded. Please be careful this.
        • 这篇关于形状大小不等于表格单元格大小,并且使文本适合形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆