Google Apps脚本:如何从文档中删除链接 [英] Google Apps Script: How to remove a link from a document

查看:163
本文介绍了Google Apps脚本:如何从文档中删除链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从Text元素中删除链接.

I'm having trouble to remove a link from a Text element.

调用.setLinkUrl(null)似乎可以,但是会使文档崩溃!您可以通过这个很小的Google脚本看到它的发生:

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);
}

先运行它,然后尝试编辑文档,将出现文件不可用"错误.还尝试设置使用.setAttributes删除链接,但出现相同的错误.

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?

谢谢!

推荐答案

现在使用setLinkUrl(null)会完全删除该链接.示例:

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);
}

这篇关于Google Apps脚本:如何从文档中删除链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆