2017-02-23 4 views
0

응용 프로그램 스크립트 사이드 바를 사용하여 텍스트를 삽입합니다. 여기서는 입력 할 때 처음에 텍스트를 추가 한 다음 다시 입력해야합니다.개행 문자를 GAS의 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에 주어진 규칙을 먼저 확인하고 삽입 할 수있는 텍스트 요소와 해당 요소를 제자리에서 조작 할 수있는 요소 만 보여주는 트리를 확인할 수 있습니다.

언급 한 것처럼 애플리케이션 스크립트의 문서 서비스는 특정 유형의 요소 만 삽입 할 수 있습니다. 트리에서 허용되는 요소를 삽입하려는 경우 insertText(offset, text)과 같은 텍스트를 삽입하는 방법에 사용할 수있는 방법을 알고 싶으면 Class 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'); 
: 여기

은 삽입 텍스트에서 샘플 코드입니다