在光标处插入一块预格式化的文本 [英] Insert a block of preformatted text at cursor
问题描述
我正在尝试使我正在编写的文档中的一些无聊的操作自动化.
我希望能够在光标位置插入如下内容:
HEADING3
某些文本
BOLD_TEXT1
2x2桌子
BOLD_TEXT2
2x2桌子
BOLD_TEXT2
2x2桌子
其中BOLD_TEXT具有相同的样式(即,文本尺寸14,灰色和所有大写字母),并且该表是页面范围的,并且第一行具有彩色(即绿色),而第二列具有第二行具有背景色./p>
有可能不生气吗?我尚未在GAS文档中找到一些有效的示例.
是的,有可能不生气.这是一个函数,该函数定位当前元素,然后在其后插入一堆东西.
找到当前元素
首先,在光标所在的位置获取元素,然后递归获取其父元素,直到达到Body级别.这是必需的,因为必须在主体中插入新的段落和表格.
定义样式
创建一个空对象,然后定义其属性,例如 I'm trying to automate some boring action in a documentation I'm writing. I would like to be able to insert at cursor position something like this: Some Text BOLD_TEXT1 2x2 table BOLD_TEXT2 2x2 table BOLD_TEXT2 2x2 table Where BOLD_TEXT has the same style (i.e. text dimension 14, grey color and all caps) and the table is page-wide and has the first row colored (i.e green) while the second column has second row with background color. Is it possible without getting mad? I have not found some valid example in GAS docs. Yes, it's possible without getting mad. Here is a function that locates the current element and then inserts a bunch of stuff after it. First, get the element at the location of the cursor, and then recursively get its parent until we reach Body level. This is necessary because new paragraphs and tables must be inserted in the Body. Create an empty object and then define its properties like Done with Body methods The only way I found to make a table full-width is by computing the required cell width from page width and margins, and setting this width on cells. This may be slightly off because cell borders have width, but I decided not to care. End result: For convenience of use, the function onOpen will create a menu item Custom > Insert Stuff every time the document is open.
这篇关于在光标处插入一块预格式化的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! DocumentApp.Attribute.BACKGROUND_COLOR
,它们是 HEADING3
Locate current element
Define a style
DocumentApp.Attribute.BACKGROUND_COLOR
, which are listed here. Apply to an element with setAttributes
method. Insert elements
insertParagraph
and insertTable
. Since the table involves a bit of custom formatting, it is separated into a function insertMyTable
. Apparently one can't apply background color to a row, so this is done at cell level. The row/column index of cells is 0-based. Full-page table
function insertStuff() {
var body = DocumentApp.getActiveDocument().getBody();
var cursor = DocumentApp.getActiveDocument().getCursor();
if (cursor) {
var element = cursor.getElement();
while (element.getParent().getType() != DocumentApp.ElementType.BODY_SECTION) {
element = element.getParent();
}
var index = body.getChildIndex(element);
}
else {
DocumentApp.getUi().alert("Could not find current position");
return;
}
var boldStyle = {};
boldStyle[DocumentApp.Attribute.BOLD] = true;
boldStyle[DocumentApp.Attribute.FOREGROUND_COLOR] = "#555555";
boldStyle[DocumentApp.Attribute.FONT_SIZE] = 14;
body.insertParagraph(index + 1, "Heading3").setHeading(DocumentApp.ParagraphHeading.HEADING3);
body.insertParagraph(index + 2, "some text");
body.insertParagraph(index + 3, "bold text 1").setAttributes(boldStyle);
insertMyTable(body, index + 4);
body.insertParagraph(index + 5, "bold text 2").setAttributes(boldStyle);
insertMyTable(body, index + 6);
body.insertParagraph(index + 7, "bold text 3").setAttributes(boldStyle);
insertMyTable(body, index + 8);
}
function insertMyTable(body, index) {
var topCellStyle = {};
topCellStyle[DocumentApp.Attribute.BACKGROUND_COLOR] = "#00FF00";
var bottomRightCellStyle = {};
bottomRightCellStyle[DocumentApp.Attribute.BACKGROUND_COLOR] = "#0000FF";
var table = body.insertTable(index, [["", ""], ["", ""]]);
table.getCell(0, 0).setAttributes(topCellStyle);
table.getCell(0, 1).setAttributes(topCellStyle);
table.getCell(1, 1).setAttributes(bottomRightCellStyle);
var cellWidth = (body.getPageWidth() - body.getMarginLeft() - body.getMarginRight())/2;
table.getCell(0, 0).setWidth(cellWidth);
table.getCell(0, 1).setWidth(cellWidth);
}
function onOpen() {
DocumentApp.getUi().createMenu("Custom").addItem("Insert stuff", "insertStuff").addToUi();
}