有没有一种方法可以将Google文档分割为多个PDF?
我想在Google Scripts VBA代码中复制我为Word文档编写的代码.基本上,它通过搜索我插入到文档中的标签将文档切片"为多个PDF文件.目的是允许使用forScore(管理乐谱的应用程序)的合唱团在切片点处插入先前注释的乐谱.这样,在表演过程中,他们可以从头到尾完整地翻阅音乐节目.
I would like to replicate in Google Scripts VBA code that I wrote for Word docs. Basically, it "slices" a document into multiple PDF files by searching for tags that I insert into the document. The purpose is to allow choirs using forScore (an app that manages musical scores) to insert previously annotated musical scores at the slice points. That way, during a performance they can page through a musical program in its entirety from start to finish.
这是我到目前为止所拥有的.我知道它是粗略的,并且实际上不能按原样工作:
This is what I have so far. I know it's sketchy, and it doesn't actually work as is:
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);
}
}
}
我认为我有解决方案.下一个脚本将在文本中搜索一个特定的单词,并且每次找到该单词时,都会在您的云端硬盘帐户中创建一个新文档,每个单词之间都将包含文本.请记住,这个词也会添加到文本中,因此,如果要删除它,我将使用该代码更新答案.
I think I have the solution for this. The next script will search for a certain word in the text, and every time it finds it it will create a new Doc in your Drive account with the text between each word. Keep in mind that this word is also added to the text, so if you want to remove it, I will update the answer with the code for that.
由于我不确定您是要下载PDF还是将其保存在云端硬盘中,它会创建链接以下载PDF格式的文件.执行完成后,您可以在查看">日志"中找到它们.
As I wasn't sure if you wanted to download the PDFs or keep them in Drive, it will create links to download those files in PDF format. You can find them in View > Logs after the execution finished.
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.如果您需要帮助,我也会更新我的代码.
In case you have too many files and want to download the PDFs automatically, you should deploy the script as a WebApp. If you need help with that I will update my code too.