我正在尝试创建一个函数,当传入Google Slides演示文稿的ID时,可以解析演示文稿并将其找到的所有URL链接写入Google表格。我已经根据@Yuval的答案适应了以下函数,以便对Google文档进行相同的操作。
当我尝试将Google幻灯片演示文稿作为输入来创建相同的内容时,我卡在如何解析文档并提取所有文本片段(以便检查它们是否含有链接)的步骤上。看起来我需要使用
function getAllLinks(docId, mergeAdjacent) {
var links = [];
//var doc = DocumentApp.getActiveDocument();
var doc = DocumentApp.openById(docId);
var parentDocName = doc.getName();
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Extracted Links');
iterateSections(doc, function(section, sectionIndex, isFirstPageSection) {
if (!("getParagraphs" in section)) {
// as we're using some undocumented API, adding this to avoid cryptic
// messages upon possible API changes.
throw new Error("An API change has caused this script to stop " +
"working.\n" +
"Section #" + sectionIndex + " of type " +
section.getType() + " has no .getParagraphs() method. " +
"Stopping script.");
}
section.getParagraphs().forEach(function(par) {
// skip empty paragraphs
if (par.getNumChildren() == 0) {
return;
}
// go over all text elements in paragraph / list-item
for (var el=par.getChild(0); el!=null; el=el.getNextSibling()) {
if (el.getType() != DocumentApp.ElementType.TEXT) {
continue;
}
// go over all styling segments in text element
var attributeIndices = el.getTextAttributeIndices();
var lastLink = null;
attributeIndices.forEach(function(startOffset, i, attributeIndices) {
var url = el.getLinkUrl(startOffset);
if (url != null) {
// we hit a link
var endOffsetInclusive = (i+1 < attributeIndices.length?
attributeIndices[i+1]-1 : null);
// check if this and the last found link are continuous
if (mergeAdjacent && lastLink != null && lastLink.url == url &&
lastLink.endOffsetInclusive == startOffset - 1) {
// this and the previous style segment are continuous
lastLink.endOffsetInclusive = endOffsetInclusive;
return;
}
lastLink = {
"section": section,
"isFirstPageSection": isFirstPageSection,
"paragraph": par,
"textEl": el,
"startOffset": startOffset,
"endOffsetInclusive": endOffsetInclusive,
"url": url
};
var row = sh.getLastRow() + 1;
var r1=sh.getRange(row, 1);
r1.setValue(parentDocName);
var r2=sh.getRange(row, 2);
r2.setValue(url);
Logger.log(parentDocName)
Logger.log(url)
links.push(lastLink);
}
});
}
});
});
return links;
}
/**
* Calls the given function for each section of the document (body, header,
* etc.). Sections are children of the DocumentElement object.
*
* @param {Document} doc The Document object (such as the one obtained via
* a call to DocumentApp.getActiveDocument()) with the sections to iterate
* over.
* @param {Function} func A callback function which will be called, for each
* section, with the following arguments (in order):
* - {ContainerElement} section - the section element
* - {Number} sectionIndex - the child index of the section, such that
* doc.getBody().getParent().getChild(sectionIndex) == section.
* - {Boolean} isFirstPageSection - whether the section is a first-page
* header/footer section.
*/
function iterateSections(doc, func) {
// get the DocumentElement interface to iterate over all sections
// this bit is undocumented API
var docEl = doc.getBody().getParent();
var regularHeaderSectionIndex = (doc.getHeader() == null? -1 :
docEl.getChildIndex(doc.getHeader()));
var regularFooterSectionIndex = (doc.getFooter() == null? -1 :
docEl.getChildIndex(doc.getFooter()));
for (var i=0; i<docEl.getNumChildren(); ++i) {
var section = docEl.getChild(i);
var sectionType = section.getType();
var uniqueSectionName;
var isFirstPageSection = (
i != regularHeaderSectionIndex &&
i != regularFooterSectionIndex &&
(sectionType == DocumentApp.ElementType.HEADER_SECTION ||
sectionType == DocumentApp.ElementType.FOOTER_SECTION));
func(section, i, isFirstPageSection);
}
}
当我尝试将Google幻灯片演示文稿作为输入来创建相同的内容时,我卡在如何解析文档并提取所有文本片段(以便检查它们是否含有链接)的步骤上。看起来我需要使用
getSlides()
,然后通过getPageElements()
迭代它们,但我不清楚如何获取幻灯片上的实际文本。如果有任何关于如何遍历幻灯片上的实际文本(以及可能如何从该文本中提取链接URL)的提示,将不胜感激。谢谢!
SpreadsheetApp
是否也有类似的link
对象吗?如果有,您知道我需要应用哪种方法来获取链接吗?如果我通过getSheets()
和getRange()
迭代电子表格的话。 - Gabriel Tero