2017-02-23 27 views
0

我使用Google Apps脚本边栏插入文本,在那里我输入需要一些文字开头追加,然后再键入追加后。传递换行符作为变量insertText气体

所附文本将通过在侧边栏文本框来确定。

我传递值formObject

function sendform(){ 
    var f = document.forms[0].elements; 
    var data = {  "mytext": f[0].value } 
    google.script.run.withSuccessHandler(ready).withFailureHandler(onFailure).processForm(data); 
} 

下面是应用脚本代码。

function processForm(fO) 
    { 
     var body = DocumentApp.getActiveDocument().getBody(); 
     body.editAsText().insertText(0, "\n\nsometext"); 
// this will perfectly insert the newlinenewlinesometext to the document 

     body.editAsText().insertText(0, fO.mytext); 
// this will insert \n\nsometext which is wrong 
    } 

我试着用encodeURIComponent方法decodeURIComponent,但它仍然同样的问题。

有什么建议吗?

回答

1

您可能需要先检查Structure of a document给出的规则,其中,你会发现树上显示哪些文本元素可以插入哪些元素只能在地方进行操作。

如上所述,文件服务应用脚本只能插入某些类型的元素。如果你在你试图插入一个允许的元素树发现,看到Class Text知道你能如何插入文本,如insertText(offset, text)使用的方法。

下面是插入文本的示例代码:

var body = DocumentApp.getActiveDocument().getBody(); 

// Use editAsText to obtain a single text element containing 
// all the characters in the document. 
var text = body.editAsText(); 

// Insert text at the beginning of the document. 
text.insertText(0, 'Inserted text.\n'); 

// Insert text at the end of the document. 
text.appendText('\nAppended text.'); 

// Make the first half of the document blue. 
text.setForegroundColor(0, text.getText().length/2, '#00FFFF');