【问题标题】:Add text and remove text in Google Docs (Add-on)在 Google 文档中添加文本和删除文本(附加组件)
【发布时间】:2016-01-08 20:51:41
【问题描述】:

我们有一个用于 Doc 的插件,它将数据插入到文档中,我们还希望它在您单击 再次 按钮时删除文本,就像一个开关一样。就像现在一样,在第二次、第三次按下按钮时,依此类推......它会在已经存在的文本上填充新文本。

客户端:

<div class="block" id="button-bar">
          <button onclick="google.script.run.insertTextA();" id="TextA">Text A</button>
          <button onclick="google.script.run.insertTextB();" id="TextB">Text B</button>   
          </div>

服务器端:

function insertTextA() {
 var body = DocumentApp.getActiveDocument().getBody();
 var text = body.editAsText();
 text.insertText(11, 'There is no one who loves pain itself, who seeks after it and wants to have it, simply because it is pain..');                         
}

function insertTextB() {
 var body = DocumentApp.getActiveDocument().getBody();
 var text = body.editAsText();
 text.insertText(12, 'Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old..');
}

【问题讨论】:

  • 您尝试删除它的目的是什么?

标签: javascript html google-apps-script google-api


【解决方案1】:

试试这个:

function insertTextA() {
 var body = DocumentApp.getActiveDocument().getBody();
 var val = body.findText('There is no one who loves pain itself, who seeks after it and wants to have it, simply because it is pain..');
  if(val == null)
  {
   var text = body.editAsText();
   text.insertText(11, 'There is no one who loves pain itself, who seeks after it and wants to have it, simply because it is pain..');         
  }
  else
  {
    body.replaceText('There is no one who loves pain itself, who seeks after it and wants to have it, simply because it is pain..','');
  }
}

【讨论】:

  • 工作得很好,感谢 iJay 为我指明了正确的方向,ofc 带有 replaceText 的 if 语句会起作用,我为什么不自己想:]
猜你喜欢
  • 2017-11-15
  • 2017-01-30
  • 1970-01-01
  • 1970-01-01
  • 2011-06-15
  • 2013-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多