我想在Google Scripts VBA代码中复制我为Word文档编写的代码。基本上,它通过搜索我插入到文档中的标签将文档“切片”为多个PDF文件。目的是允许使用forScore(管理乐谱的应用程序)的合唱团在切片点处插入先前注释的乐谱。这样,在表演期间,他们可以从头到尾完整地翻阅音乐节目。
这是我到目前为止所拥有的。我知道它是粗略的,并且实际上不能按原样工作:
enter code here
// Loop through body elements searching for the slice tags
// Slice tag example: #LCH-Introit (Seven Seasonal-God is Ascended)#
// When a slice tag is found a document is created with a subsection of the document
// NOT SURE USING RANGES IS WHAT'S NEEDED
rangeBuilder = doc.newRange();
for (i = 0; i < body.getNumChildren(); i++) {
child = body.getChild(i);
rangeBuilder.addElement(child);
// Logger.log(fName + ": element(" + i + ") type = " + child.getType());
if (child.getType() === DocumentApp.ElementType.PARAGRAPH) {
foundElement = child.findText('#LCH-');
if (foundElement) {
txt = foundElement.getElement().getParent().editAsText().getText();
// Generate PDF file name
count++;
padNum = padDigits(count, 2); // Function I wrote to pad zeroes
pdfName = serviceName + '-' + padNum + ' ' + txt.substr(txt.indexOf('-') + 1, (txt.length - txt.indexOf('-') - 2));
Logger.log(fName + ": pdfName = " + pdfName);
// Create new doc and populate with current Elements
// Here's where I'm stuck.
// There doesn't seem to be a way to reference page numbers in Google Scripts.
// That would make things much easier.
// docTemp = DocumentApp.create(pdfName);
}
}
}
答案 0 :(得分:1)
我想我有解决方案。下一个脚本将在文本中搜索一个特定的单词,并且每次找到该单词时,都会在您的云端硬盘帐户中创建一个新文档,每个单词之间都将包含文本。请记住,这个词也会添加到文本中,因此,如果您要删除它,我将使用该代码更新答案。
由于我不确定您是要下载PDF还是将其保存在云端硬盘中,因此它将创建链接以将这些文件下载为PDF格式。执行完成后,您可以在“查看”>“日志”中找到它们。
function main() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var listItems = body.getListItems();
var docsID = []
var content = [];
for (var i = 0; i < listItems.length; i++){ //Iterates over all the items of the file
content[i] = listItems[i].getText(); //Saves the text into the array we will use to fill the new doc
if (listItems[i].findText("TextToFind")){
createDoc(content, i, docsID);
content = []; //We empty the array to start over
}
}
linksToPDF(docsID);
}
function createDoc(content, index, docsID){
var newdoc = DocumentApp.create("Doc number " + index); //Creates a new doc for every segment of text
var id = newdoc.getId();
docsID.push(id);
var newbody = DocumentApp.openById(id).getBody();
for (i in content){
newbody.appendListItem(content[i]); //Fills the doc with text
}
}
function linksToPDF(docsID){ //
Logger.log("PDF Links: ");
for (i in docsID){
var file = DriveApp.getFileById(docsID[i]);
Logger.log("https://docs.google.com/document/d/" + docsID[i] + "/export?format=pdf&");
}
}
如果文件太多并且想要自动下载PDF,则应将脚本部署为WebApp。如果您需要帮助,我也会更新我的代码。