我正在使用应用程序脚本侧边栏来插入文本,我输入的地方需要在开始时附加一些文本,然后在再次键入附录后。
附录文本将由侧边栏中的文本框确定。
i将值作为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, "nnsometext");
// this will perfectly insert the newlinenewlinesometext to the document
body.editAsText().insertText(0, fO.mytext);
// this will insert nnsometext which is wrong
}
我尝试使用Encodeuricomponent decodeuricomponent,但仍然存在相同的问题。
有什么建议?
您可能要首先检查文档结构中给出的规则,其中您会发现可以插入哪些文本元素,并且只能在适当的位置操纵哪些元素。
如前所述,应用程序脚本中的文档服务只能插入某些类型的元素。如果您在树上发现要插入允许元素,请参见类文本以了解可以使用如何插入文本(例如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');