Google Apps Script: How to remove a link from a document

别等时光非礼了梦想. 提交于 2020-01-06 18:08:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!