问题
I'm having trouble to remove a link from a Text element.
Calling .setLinkUrl(null)
on it seems to work, but makes the document crash! You can see it happening with this tiny google script:
function test() {
var body = DocumentApp.getActiveDocument().getBody();
var text = body.appendParagraph("link").editAsText();
text.setLinkUrl(null);
}
Running it then trying to edit the document gives me a "File unavailable" error. Also tried to set remove the link with .setAttributes
, but same error.
Is there a way to remove a link or just generally reset the styles on a text element?
Thanks!
回答1:
I found an horrible trick to remove LinkURLs from text, it made the trick for me :)
var textRangeElement = body.findText("yourpattern");
var textElement = textRangeElement.getElement().getText();
textRangeElement.getElement().setText(textElement);
回答2:
Using setLinkUrl(null) now fully removes the link. Example:
function myFunction() {
var a = DocumentApp.getActiveDocument();
a.getBody().editAsText().appendText('abc');
var b = a.getBody().findText('abc');
b.getElement().asText().setLinkUrl('https://google.com');
}
function myFunction2() {
var a = DocumentApp.getActiveDocument();
var b = a.getBody().findText('abc');
b.getElement().asText().setLinkUrl(null);
}
来源:https://stackoverflow.com/questions/26729517/google-apps-script-how-to-remove-a-link-from-a-document