我有一个文本与链接,这是通过用户的行动插入从谷歌插件,用户可以插入更多的文本,同时与插件,所以我如何插入之间的连续文本插入的空间。
当前场景:
[Text1][Text2][Text3]我想要的是:
[Text1] [Text2] [Text3]当前插入代码:
cursor = doc.getCursor();
if (cursor) {
var text = cursor.insertText(textStr);
if (text) {
text.setLinkUrl(url);
text.setUnderline(false);
text.setForegroundColor('#000000');
var len = text.getText().length - 1;
try {
var pos = doc.newPosition(text, cursor.getOffset() + Number(len));
doc.setCursor(pos);
}
catch (ex) {
var pos = doc.newPosition(text, cursor.getOffset() + Number(len) - 1);
doc.setCursor(pos);
}
}
else {
DocumentApp.getUi().alert('Cannot insert text here.');
}
}
else {
DocumentApp.getUi().alert('Cannot find cursor.');
}发布于 2019-08-26 20:59:28
您插入了两次textStr
因为使用var text = cursor.insertText(textStr);和text.setLinkUrl(url);等同于调用insertText(textStr)方法两次。
在两个截面之间插入空格的步骤
可以使用cursor.insertText(" ");在textStr之后插入一个仅包含空格的字符串
这里介绍了如何以一种简单的方式修改代码,以便在textStr后加入一个空格
function myFunction() {
var doc=DocumentApp.getActiveDocument();
var textStr='I am a text';
var url='https://stackoverflow.com';
cursor = doc.getCursor();
if (cursor) {
var text=cursor.insertText(textStr).setLinkUrl(url);
if (text) {
text.setUnderline(false);
text.setForegroundColor('#000000');
var len = text.getText().length;
var pos = doc.newPosition(text, Number(len));
doc.setCursor(pos);
var space=cursor.insertText(" ");
pos = doc.newPosition(space, 1);
doc.setCursor(pos);
}
else {
DocumentApp.getUi().alert('Cannot insert text here.');
}
}
else {
DocumentApp.getUi().alert('Cannot find cursor.');
}
}https://stackoverflow.com/questions/57653547
复制相似问题