在光标处插入一段预格式化的文本

Insert a block of preformatted text at cursor

我正在尝试在我正在编写的文档中自动执行一些无聊的操作。

我希望能够在光标位置插入如下内容:

标题 3

一些文字

BOLD_TEXT1

2x2 table

BOLD_TEXT2

2x2 table

BOLD_TEXT2

2x2 table

其中 BOLD_TEXT 具有相同的样式(即文本维度 14、灰色和全部大写),而 table 是 page-wide 并且第一行是彩色的(即绿色)而第二列的第二行带有背景色。

不生气可能吗?我没有在 GAS 文档中找到一些有效的示例。

插入带格式的文本

This is a simple solution for inserting formatted text into a document. It also includes the html for a simple side bar with a text area and submit button.

这是 gs 代码:

function insertTextAtCursor(txt)
{
  var retAddrStyle={};
  retAddrStyle[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
  retAddrStyle[DocumentApp.Attribute.FONT_SIZE] = 14;
  retAddrStyle[DocumentApp.Attribute.BOLD] = true;
  retAddrStyle[DocumentApp.Attribute.LINE_SPACING]=1;
  retAddrStyle[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT]=DocumentApp.HorizontalAlignment.LEFT;
  retAddrStyle[DocumentApp.Attribute.MARGIN_TOP]=0;
  var doc=DocumentApp.getActiveDocument().getCursor().insertText(txt).setAttributes(retAddrStyle);
  return true;
}

这将显示边栏:

function loadParagraphSidebar()
{
  var html=HtmlService.createHtmlOutputFromFile('inserttext').setTitle('Insert Text');
  DocumentApp.getUi().showSidebar(html);
}

这是边栏 (inserttext.html) 的 html:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  <script>
    function sendText()
    {
      var txt=$('#txt1').val();
      $('#txt1').css('background-color','#ffff00');
      google.script.run
        .withSuccessHandler(clearText)
        .insertTextAtCursor(txt);
    }
    function clearText()
    {
      $('#txt1').val('');
      $('#txt1').css('background-color','#ffffff');
    }
    console.log("My code");
  </script>
  </head>
  <body>
  <br />Text:<br /><textarea id="txt1" rows="12" cols="35"></textarea>
  <br /><input id="btn1" type="button" value="submit" onClick="sendText();" />    
  </body>
</html>

The Sidebar:

Before insertion:

After insertion:

是的,不生气是可能的。这是一个定位当前元素然后在它后面插入一堆东西的函数。

定位当前元素

首先获取光标所在位置的元素,然后递归获取其父元素,直到我们到达Body层。这是必要的,因为必须在正文中插入新的段落和 tables。

定义样式

创建一个空对象,然后像DocumentApp.Attribute.BACKGROUND_COLOR一样定义它的属性,即listed here。使用 setAttributes 方法应用于元素。

插入元素

完成 Body 方法 insertParagraphinsertTable。由于 table 涉及一些自定义格式,因此它被分成一个函数 insertMyTable。显然不能将背景颜色应用于一行,因此这是在单元格级别完成的。单元格的 row/column 索引从 0 开始。

整页table

我发现制作 table 全角的唯一方法是根据页面宽度和页边距计算所需的单元格宽度,然后在单元格上设置此宽度。这可能会稍微偏离,因为单元格边框有宽度,但我决定不在乎。

最终结果:

为了方便使用,onOpen函数会在每次打开文档时创建一个菜单项Custom > Insert Stuff。

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