gpt4 book ai didi

google-apps-script - 获取文档中的所有链接

转载 作者:行者123 更新时间:2023-12-03 12:43:32 25 4
gpt4 key购买 nike

给定 Google Docs/Drive 中的“普通文档”(例如段落、列表、表格),其中包含分散在整个内容中的外部链接,您如何使用 Google Apps 脚本编译存在的链接列表?

具体来说,我想通过在每个 url 中搜索 oldText 并在每个 url 中用 newText 替换它来更新文档中的所有损坏的链接,而不是文本。

我不认为 replacing text开发文档的部分是我需要的——我需要扫描文档的每个元素吗?我可以吗editAsText并使用 html 正则表达式?示例将不胜感激。

最佳答案

这只是主要是痛苦的!代码可用 as part of a gist .
Screenshot是的,我不会拼写。
获取所有链接
这是一个实用函数,它扫描文档中的所有 LinkUrls,将它们以数组形式返回。

/**
* Get an array of all LinkUrls in the document. The function is
* recursive, and if no element is provided, it will default to
* the active document's Body element.
*
* @param {Element} element The document element to operate on.
* .
* @returns {Array} Array of objects, vis
* {element,
* startOffset,
* endOffsetInclusive,
* url}
*/
function getAllLinks(element) {
var links = [];
element = element || DocumentApp.getActiveDocument().getBody();

if (element.getType() === DocumentApp.ElementType.TEXT) {
var textObj = element.editAsText();
var text = element.getText();
var inUrl = false;
for (var ch=0; ch < text.length; ch++) {
var url = textObj.getLinkUrl(ch);
if (url != null) {
if (!inUrl) {
// We are now!
inUrl = true;
var curUrl = {};
curUrl.element = element;
curUrl.url = String( url ); // grab a copy
curUrl.startOffset = ch;
}
else {
curUrl.endOffsetInclusive = ch;
}
}
else {
if (inUrl) {
// Not any more, we're not.
inUrl = false;
links.push(curUrl); // add to links
curUrl = {};
}
}
}
if (inUrl) {
// in case the link ends on the same char that the element does
links.push(curUrl);
}
}
else {
var numChildren = element.getNumChildren();
for (var i=0; i<numChildren; i++) {
links = links.concat(getAllLinks(element.getChild(i)));
}
}

return links;
}
查找和替换链接
此实用程序基于 getAllLinks做一个查找和替换功能。
/**
* Replace all or part of UrlLinks in the document.
*
* @param {String} searchPattern the regex pattern to search for
* @param {String} replacement the text to use as replacement
*
* @returns {Number} number of Urls changed
*/
function findAndReplaceLinks(searchPattern,replacement) {
var links = getAllLinks();
var numChanged = 0;

for (var l=0; l<links.length; l++) {
var link = links[l];
if (link.url.match(searchPattern)) {
// This link needs to be changed
var newUrl = link.url.replace(searchPattern,replacement);
link.element.setLinkUrl(link.startOffset, link.endOffsetInclusive, newUrl);
numChanged++
}
}
return numChanged;
}
演示界面
为了演示这些实用程序的使用,这里有几个 UI 扩展:
function onOpen() {
// Add a menu with some items, some separators, and a sub-menu.
DocumentApp.getUi().createMenu('Utils')
.addItem('List Links', 'sidebarLinks')
.addItem('Replace Link Text', 'searchReplaceLinks')
.addToUi();
}

function searchReplaceLinks() {
var ui = DocumentApp.getUi();
var app = UiApp.createApplication()
.setWidth(250)
.setHeight(100)
.setTitle('Change Url text');
var form = app.createFormPanel();
var flow = app.createFlowPanel();
flow.add(app.createLabel("Find: "));
flow.add(app.createTextBox().setName("searchPattern"));
flow.add(app.createLabel("Replace: "));
flow.add(app.createTextBox().setName("replacement"));
var handler = app.createServerHandler('myClickHandler');
flow.add(app.createSubmitButton("Submit").addClickHandler(handler));
form.add(flow);
app.add(form);
ui.showDialog(app);
}

// ClickHandler to close dialog
function myClickHandler(e) {
var app = UiApp.getActiveApplication();

app.close();
return app;
}

function doPost(e) {
var numChanged = findAndReplaceLinks(e.parameter.searchPattern,e.parameter.replacement);
var ui = DocumentApp.getUi();
var app = UiApp.createApplication();

sidebarLinks(); // Update list

var result = DocumentApp.getUi().alert(
'Results',
"Changed "+numChanged+" urls.",
DocumentApp.getUi().ButtonSet.OK);
}


/**
* Shows a custom HTML user interface in a sidebar in the Google Docs editor.
*/
function sidebarLinks() {
var links = getAllLinks();
var sidebar = HtmlService
.createHtmlOutput()
.setTitle('URL Links')
.setWidth(350 /* pixels */);

// Display list of links, url only.
for (var l=0; l<links.length; l++) {
var link = links[l];
sidebar.append('<p>'+link.url);
}

DocumentApp.getUi().showSidebar(sidebar);
}

关于google-apps-script - 获取文档中的所有链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18727341/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com