我们有一个用于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..');
}
答案 0 :(得分:2)
试试这个:
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..','');
}
}