在Google脚本中设置文本方向 [英] Setting Text Direction in Google Scripts

查看:80
本文介绍了在Google脚本中设置文本方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Docs开发一个插件,需要从右到左设置一些希伯来语文本.我尝试将 setLeftRight函数与'true'和'false',但没人会以正确的方式设置文本.
如何将文本方向设置为RTL?

I am developing an addon for Docs, and need to set some Hebrew text right-to-left. I tried using the setLeftRight function with both 'true' and 'false', but none would set the text the right way.
How can I set the text direction to RTL?

示例代码:

Code: var cells = [ ["EnglishTitle", "HebrewTitle"], ["Text", ""] ]; 
var tableStyle = {}; 
tableStyle[DocumentApp.Attribute.FONT_FAMILY] = DocumentApp.FontFamily.TIMES_NEW_ROMAN;
tableStyle[DocumentApp.Attribute.FONT_SIZE] = 12; 
var doc = DocumentApp.getActiveDocument().getBody();
doc.appendTable(cells).setAttributes(tableStyle).getCell(1,1).insertParagraph(0, "HebrewText").setLeftToRight(false);

推荐答案

此代码可将文本从右到左设置:

This code works to set the text right to left:

function setRightToLeft() {
 var doc = DocumentApp.getActiveDocument();
 var body = doc.getBody();
 var par = body.getParagraphs();
 par[0].setLeftToRight(false);
}

注意:您需要指定要从右到左设置的特定段落,或循环浏览所有段落以完全设置它们.

Note: You need to specify the specific paragraph that you want set right to left, or loop through all paragraphs to set them all that way.

正如您在下面提到的那样,转义字符'\ n'似乎将右至左文本搞乱了,这似乎是一个问题.我已记录了该问题

As you've mentioned below, there appears to be an issue with the escape character '\n' mucking up the right to left text. I've documented the issue here for review by Google, and hopefully they'll pick it up, but you'll need to star it to see if it gains any traction.

这里的解决方法是不使用'.insertParagraph()'方法中的转义字符.取而代之的是,仅将其用于插入段落,然后使用".appendText()"方法添加文本.例如,这对于在'.appendText()'方法中添加的所有内容都可以正常工作:

A workaround here is not use the escape character in the the '.insertParagraph()' method. Instead, use that only to insert the paragraph, then use the '.appendText()' method to add the text. For example, this works correctly for everything added in the '.appendText()' method:

function table(){
  Code: var cells = [ ["EnglishTitle", "HebrewTitle"], ["Text", ""] ]; 
  var tableStyle = {}; 
  tableStyle[DocumentApp.Attribute.FONT_FAMILY] = DocumentApp.FontFamily.TIMES_NEW_ROMAN;
  tableStyle[DocumentApp.Attribute.FONT_SIZE] = 12; 
  var doc = DocumentApp.getActiveDocument().getBody();
  doc.appendTable(cells).setAttributes(tableStyle).getCell(1,1).insertParagraph(0, "Top line not moved. \nText Should Be Right to left, but it's not, \nThis text is correctly on the right.").setLeftToRight(false).appendText('\nThis is the appended text,\ncorrectly set right to left.');
}

这篇关于在Google脚本中设置文本方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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