我想用Google Script从本地Google Drive上的图片文件夹中创建一个Google Slides演示文稿。我想给每张图片分别打上标签,使它们在演示文稿中具有特定的顺序,并附加一个布尔值供以后使用。
我首先尝试改编Google Developers视频中的示例代码。https:/www.youtube.comwatch?v=rTDz-Zcn7yk.
这工作,但他只是暗示能够从个人的谷歌驱动器显示图像,而没有解释如何。
然后,我试着将谷歌开发者的代码与学习谷歌电子表格视频创建的代码结合起来。https:/www.youtube.comwatch?v=PTOKHjVkXYs.
在这里,他试图从一个包含图片的本地Google驱动器文件夹中创建一个包含信息的电子表格。这让我更接近答案,但我遇到了类似的麻烦,试图将文件夹的图像传递到一个数组中,并通过.insertImage()传递它们。
以下是我试图通过尝试将这两者结合起来创建的代码。我假设我没有从我的getMyFiles函数中获取正确的数据并将它们复制到我的AfileLabels数组中。也许应该使用一个可以从我的本地驱动器返回imageUrl的.get来代替.getName,但我不知道如何做到这一点。我似乎也无法将该数组传递到我的最后一行:images.forEach(addImageSlide);。我也确信有一种比我这样反复制作fileLabel数组更简单的方法,但我不知道如何做。
var NAME = "My Presentation";
var deck = SlidesApp.create(NAME);
function getMyFiles() {
var myFolders = DriveApp;
var i = 0;
var j = 0;
var AfileLabels = new Array();
var AfileLabels = [];
var BfileLabels = new Array();
var BfileLabels = [];
var AfolderIter = myFolders.getFoldersByName("A");
var Afolder = AfolderIter.next();
var AfolderIter = Afolder.getFiles();
while(AfolderIter.hasNext()) {
var Afile = AfolderIter.next();
var AfileName = Afile.getName();
Logger.log(AfileName);
AfileLabels[i] = AfileName;
i++;
}
var i = 0;
Logger.log(AfileLabels);
var BfolderIter = myFolders.getFoldersByName("B");
var Bfolder = BfolderIter.next();
var BfolderIter = Bfolder.getFiles();
while(BfolderIter.hasNext()) {
var Bfile = BfolderIter.next();
var BfileName = Bfile.getName();
Logger.log(BfileName);
BfileLabels[i] = BfileName;
j++;
}
var j = 0;
Logger.log(BfileLabels);
}
function addImageSlide(AfileLabels, index) {
var slide = presentation.appendSlide(SlidesApp.PredefinedLayout.BLANK);
var image = slide.insertImage(AfileLabels);
var imgWidth = image.getWidth();
var imgHeight = image.getHeight();
var pageWidth = presentation.getPageWidth();
var pageHeight = presentation.getPageHeight();
var newX = pageWidth/2. - imgWidth/2.;
var newY = pageHeight/2. - imgHeight/2.;
image.setLeft(newX).setTop(newY);
}
function main() {
var AfileLabels = new Array();
var AfileLabels = [];
var AfileLabels = getMyFiles();
Logger.log(AfileLabels);
var images = ASfileLabels;
var [title, subtitle] = deck.getSlides()[0].getPageElements();
title.asShape().getText().setText(NAME);
subtitle.asShape().getText().setText("Subtitle for Presentation");
images.forEach(addImageSlide);
}
我有什么函数用错了?我如何将我的图片数组传递给图片?
最重要的。
addImageSlides()
deck
和 presentation
while
环路var NAME = "My Presentation";
var deck = SlidesApp.create("NAME");
var presentation = deck;
function getMyFiles() {
var labels = [];
var myFolders = DriveApp;
var AfolderIter = myFolders.getFoldersByName("A");
var Afolder = AfolderIter.next();
var AfolderIter = Afolder.getFiles();
while(AfolderIter.hasNext()) {
var Afile = AfolderIter.next();
var AfileId = Afile.getId();
labels.push(AfileId);
}
var BfolderIter = myFolders.getFoldersByName("B");
var Bfolder = BfolderIter.next();
var BfolderIter = Bfolder.getFiles();
while(BfolderIter.hasNext()) {
var Bfile = BfolderIter.next();
var BfileId = Bfile.getId();
labels.push(BfileId);
}
Logger.log(labels);
return labels;
}
function addImageSlide(fileId) {
var slide = presentation.appendSlide(SlidesApp.PredefinedLayout.BLANK);
var image = slide.insertImage(DriveApp.getFileById(fileId));
var imgWidth = image.getWidth();
var imgHeight = image.getHeight();
var pageWidth = presentation.getPageWidth();
var pageHeight = presentation.getPageHeight();
var newX = pageWidth/2. - imgWidth/2.;
var newY = pageHeight/2. - imgHeight/2.;
image.setLeft(newX).setTop(newY);
}
function main() {
var fileIds = getMyFiles();
Logger.log(fileIds);
var [title, subtitle] = deck.getSlides()[0].getPageElements();
title.asShape().getText().setText(NAME);
subtitle.asShape().getText().setText("Subtitle for Presentation");
for(var i = 0; i< fileIds.length; i++){
addImageSlide(fileIds[i]);
}
}