在光标位置插入一张表格到Google文档中 [英] Insert a table into google doc at cursor position

查看:269
本文介绍了在光标位置插入一张表格到Google文档中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Google Docs附加功能,将文本转化为表格。它允许用户1)选择一些文本或2)将光标放置在他们想要的文本内,并且当单击边栏中的自定义按钮时,该脚本将会:


  1. 在光标位置或选定文本的开始索引处插入单行,单个单元格表格

  2. 将单元格放入该单元格中,已选择或放置光标的元素的完整文本

  3. 删除原始选定的文本/元素,以便仅保留表格

  4. ol>

    (本质上,这只是在选定的文本或元素周围绘制一个表格)



    文档显示可以通过 var element = cursor.insertText('ಠ‿ಠ'); ,但是没有类似的插入其他元素的方法。因此,我一直试图使用不同程度成功的 insertTable(childIndex,cells)。关注用户将光标置于元素内部的情况,我可以插入一个正确文本的表格并删除原始文本,但是我找不到将表格插入正确位置的方法。我已经尝试了以下,无济于事:


    1. var newTable = body.insertTable(cursor。 getOffset(),[['Test Text']]); - 这会创建表格,但会将其插入错误的位置,这似乎基于光标所在文本的位置,通常朝向开头


    2. var el = cursor.getElement()。asBody();
      var newTable = el.insertTable(0,[['Test Text']]);
      - 没有任何操作

    3. p> var el = cursor.getElement()。getParent()。asBody();
      var newTable = el.insertTable(0,[['Test Text']]);
      - 没有任何操作 p> var el = cursor.getElement();
      var parent = el.getParent()。getChildIndex(el);
      var newTable = body.insertTable(parent,[['Test Text']]);
      - 在表格的最开始处插入表格。


    Stack Overflow的搜索产生,建议在光标位置插入一些占位符文本,然后在文档中搜索该插入的字符串并放置一个表格。这种方法有两个主要问题:


    1. 我需要直接修改插入点周围的文本,插入额外的文本就好像它可能会让这个变得更加困难,而且
    2. 更重要的是,我仍然不知道如何获得表格插入点的正确位置。

    3. ol>

      如果任何人都可以扩展占位符文本方法,或者可以提供新的解决方案,那将非常有帮助。感谢!

      解决方案

      我使用以下GAS代码在Google文档的当前光标位置插入表格。如果你的游标位于换行符上,它会更好。

        var doc = DocumentApp.getActiveDocument(); 
      var body = doc.getBody();
      var cursor = doc.getCursor();

      var element = cursor.getElement();
      var parent = element.getParent();
      //这里的表格是一个像
      // [[r1c1,r1c2],[r2c1,r2c2]]
      body.insertTable(parent.getChildIndex(element)+ 1,表);

      编辑:为了更好地了解Google文档中光标位置的运作情况,我建议您运行以下游标检查器github项目



      https://github.com/google/google-apps-script-samples/tree/master/cursor_inspector


      I am working on a Google Docs add on that turns text into a table. It allows the user to either 1) select some text or 2) place their cursor inside the text they want, and when a custom button in the sidebar is clicked, the script will:

      1. Insert a single row, single cell table at the cursor position or the startIndex of the selected text,
      2. Place inside that cell either the text a user has selected or the complete text of the element in which the cursor is placed,
      3. Delete the original selected text/element so that only the table remains

      (Essentially, this is just 'drawing' a table around the selected text or element)

      An overview of the documentation shows that one can insert text via the var element = cursor.insertText('ಠ‿ಠ');, but there is no similar method for inserting other elements. As such, I have been trying to use the insertTable(childIndex, cells) with varying degrees of success. Focusing on the circumstance in which a user simply places the cursor inside the element, I can insert a table with the correct text and delete the original text, but I cannot find a way to insert the table at the correct position. I have tried the following, to no avail:

      1. var newTable = body.insertTable(cursor.getOffset(), [['Test Text']]); - This creates the table but inserts it at the wrong position, seemingly based on where in the text the cursor is placed, usually towards the beginning of the document.

      2. var el = cursor.getElement().asBody(); var newTable = el.insertTable(0, [['Test Text']]); - This does nothing

      3. var el = cursor.getElement().getParent().asBody(); var newTable = el.insertTable(0, [['Test Text']]); - This does nothing

      4. var el = cursor.getElement(); var parent = el.getParent().getChildIndex(el); var newTable = body.insertTable(parent, [['Test Text']]); - This inserts the table at the very beginning of the document.

      A search of Stack Overflow yields this very similar question with the suggestion of inserting some placeholder text at the cursor position, then searching the document for that inserted string and placing a table. I have two main concerns with this approach:

      1. I need to modify the text directly surrounding the insert point, and inserting extra text seems like it could make this more difficult, and
      2. More importantly, I still do not know how to obtain the correct position for the insert point of the table.

      If anyone could expand upon the placeholder-text-method, or could provide a new solution, that would be very helpful. Thanks!

      解决方案

      I used the following GAS code to insert table at current cursor position in Google Doc. It works better if your cursor is on a newline.

        var doc = DocumentApp.getActiveDocument();
        var body = doc.getBody();
        var cursor = doc.getCursor();
      
        var element = cursor.getElement();
        var parent = element.getParent();
        //here table is an array of cells like
        // [[r1c1, r1c2], [r2c1, r2c2]]
        body.insertTable(parent.getChildIndex(element) + 1, table);
      

      Edit: To get a better insight in workings of cursor position in Google Doc, I recommend you to run the following cursor inspector github project

      https://github.com/google/google-apps-script-samples/tree/master/cursor_inspector

      这篇关于在光标位置插入一张表格到Google文档中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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