2016-01-08 2 views
0

У нас есть надстройка для документов, где она вставляет данные в документ, мы также хотели бы удалить текст, когда вы снова нажмете кнопку , как переключатель. Как сейчас, при нажатии на кнопку второй раз, в третий раз и т. Д., Он заполняет новый текст над уже существующим.Добавить текст и удалить текст в Документах Google (Add-on)

enter image description here

стороне клиента:

<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..',''); 
    } 
} 
+1

Работал отлично, спасибо iJay за то, что указал мне в правильном направлении, из if-statement с replaceText будет работать, почему я не подумал об этом сам:] –

Смежные вопросы