英文:
How to export Google Slide as png after importing image to it from Google Form - Apps Script Image from Form to Slide and export to png
问题
function onFormSubmit(e) {
// Open the template presentation by ID
var templateDoc = DriveApp.getFileById('1bGKJ027bnUCgSpQrunXXBZCgFNXZHtF2ZnsaWHLh8B4');
// Create a copy of the template to avoid messing it up
var newTempFile = templateDoc.makeCopy();
// Open the presentation for editing
var openSlide = SlidesApp.openById(newTempFile.getId());
// Get the responses triggered by On Form Submit
var items = e.response.getItemResponses();
// Find the text in the presentation and replace it with the Form response
var image = items[0].getResponse();
openSlide.getSlides().forEach(function(slide) {
slide.getShapes().forEach(function(shape) {
if (shape.getText().asString().trim() === '{{image1}}') {
shape.replaceWithImage(DriveApp.getFileById(Array.isArray(image) ? image[0] : image).getBlob());
}
});
});
// Save and close the open document
DriveApp.getFileById(newTempFile.getId()).setName(items[0].getResponse());
}
// Code for making PNG file from a slide
var folderId = "xxxxxxxxxxxxxxxxxxxx"; // Change to your folder ID
var presentationId = "xxxxxxxxxxxxxxxxxxx"; // Change to your presentation ID
function convertToPNG() {
var slideId = 'p'; // You need to specify the slide ID here
var url = 'https://docs.google.com/presentation/d/' + presentationId +
'/export/png?id=' + presentationId + '&pageid=' + slideId;
var options = {
headers: {
Authorization: 'Bearer ' + ScriptApp.getOAuthToken()
}
};
var response = UrlFetchApp.fetch(url, options);
var image = response.getAs(MimeType.PNG);
image.setName("png-" + Math.random().toFixed(2));
var img = DriveApp.getFolderById(folderId).createFile(image).getUrl();
Logger.log(img);
}
In your code, you need to specify the slideId
in the convertToPNG
function, which tells it which slide to convert to a PNG file. You will need to provide the correct slide ID where you want to create a PNG file. The rest of the code should work as expected.
英文:
Hello i try to make simple template file for mixing toegether two images.
One is as overlay of slide and second one come from Google Form as background.
Problem is that i don't have any knowledge about this code.
I stuck at this point and i cant combine this two piece of code.
function onFormSubmit(e) {
//open the template presentation by ID
var templateDoc = DriveApp.getFileById('1bGKJ027bnUCgSpQrunXXBZCgFNXZHtF2ZnsaWHLh8B4');
//create a copy of the template, we don't wanna mess up the template presentation
var newTempFile = templateDoc.makeCopy();
//open the presentation for editing
var openSlide = SlidesApp.openById(newTempFile.getId());
//get the responses triggered by On Form Submit
var items = e.response.getItemResponses();
//find the text in the presentation and replace it with the Form response
//items[0].getResponse() is the first response in the Form
//and it is the "Title"
//openSlide.replaceAllText('{Title}', items[0].getResponse());
//items[1].getResponse() is the second and it is the date
//openSlide.replaceAllText('{Text}', items[1].getResponse());
//You can add as much as you have and change them in the Template Doc like this
//openSlide.replaceAllText('{number}', items[2].getResponse());
//openSlide.replaceAllText('{choice}', items[3].getResponse());
//and so on...
var image = items[0].getResponse();
openSlide.getSlides().forEach(s => {
s.getShapes().forEach(e => {
if (e.getText().asString().trim() == '{{image1}}') {
e.replaceWithImage(DriveApp.getFileById(Array.isArray(image) ? image[0] : image).getBlob());
}
})
});
//Save and Close the open document
DriveApp.getFileById(newTempFile.getId()).setName(items[0].getResponse());
}
// this is code for making PNG file from slide, it work like a charm when its running in simple slide file with slide id pasted in presentationId (below) but i cant figure it out how to present the id of new (above) just created slide to code below. folderId is easy i think. But problem is with this presentationId
var folderId = "xxxxxxxxxxxxxxxxxxxx"//เปลี่ยน ไอดีโฟลเดอร์เป็นของท่านเอง
var presentationId = "xxxxxxxxxxxxxxxxxxx"//เปลี่ยน ไอดี Slide เป็นของท่านเอง
function convertToPNG() {
var slideId = 'p';
var url = 'https://docs.google.com/presentation/d/' + presentationId +
'/export/png?id=' + presentationId + '&pageid=' + slideId;
var options = {
headers: {
Authorization: 'Bearer ' + ScriptApp.getOAuthToken()
}
};
var response = UrlFetchApp.fetch(url, options);
var image = response.getAs(MimeType.PNG);
image.setName("png-"+Math.random().toFixed(2));
//image.setName(DriveApp.getFileById(presentationId).getName());
var img = DriveApp.getFolderById(folderId).createFile(image).getUrl()
Logger.log(img)
}
I try my best to combine these two codes but nothing works for me (because I have zero knowledge of this scripting language)
Can someone try to give me a hint on how to tell the second part of the code to know from what SlideId should make a png file?
答案1
得分: 0
以下是您提供的代码的翻译部分:
// 在表单提交时触发的函数
function onFormSubmit(e) {
// 通过ID打开模板演示文稿
var templateDoc = SlidesApp.openById('1bGKJ027bnUCgSpQrunXXBZCgFNXZHtF2ZnsaWHLh8B4');
// 创建模板的副本,以免破坏模板演示文稿
var newTempFile = DriveApp.getFileById(templateDoc.getId()).makeCopy();
// 打开新演示文稿以进行编辑
var newSlide = SlidesApp.openById(newTempFile.getId());
// 获取由表单提交触发的响应
var items = e.response.getItemResponses();
// 查找演示文稿中的文本并用表单响应替换它
// Items[0].getResponse() 是表单中的第一个响应,通常是 "Title"
// 使用 newSlide.replaceAllText('{Title}', items[0].getResponse()) 进行替换
// Items[1].getResponse() 是第二个响应,通常是日期
// 使用 newSlide.replaceAllText('{Text}', items[1].getResponse()) 进行替换
// 您可以添加更多的替换,根据模板文档中的需要
// 例如:newSlide.replaceAllText('{number}', items[2].getResponse());
// newSlide.replaceAllText('{choice}', items[3].getResponse());
// 以此类推...
var image = items[0].getResponse();
newSlide.getSlides().forEach(s => {
s.getShapes().forEach(e => {
if (e.getText().asString().trim() == '{{image1}}') {
e.replaceWithImage(DriveApp.getFileById(Array.isArray(image) ? image[0] : image).getBlob());
}
})
});
// 保存并关闭新文档
newSlide.saveAndClose();
// 将新幻灯片另存为PDF
var newSlideFile = DriveApp.getFileById(newTempFile.getId());
var pdfBlob = newSlideFile.getAs('application/pdf');
var pdfName = items[0].getResponse() + '.pdf';
var folder = DriveApp.getFolderById('1lEP4qtXs6fITXS9GKeaYKLfbRONgbuOQ');
folder.createFile(pdfBlob.setName(pdfName));
var folderId = "10XCnKfhk4_5agWCYxWHGCO6wwxWSG7Y9";
// 将新幻灯片另存为PNG文件
var slideId = newSlide.getSlides()[0].getObjectId();
var url = 'https://docs.google.com/presentation/d/' + newSlide.getId() + '/export/png?id=' + newSlide.getId() + '&pageid=' + slideId;
var options = {
headers: {
Authorization: 'Bearer ' + ScriptApp.getOAuthToken()
}
};
var response = UrlFetchApp.fetch(url, options);
var image = response.getAs(MimeType.PNG);
image.setName("png-" + Math.random().toFixed(2));
var img = DriveApp.getFolderById(folderId).createFile(image).getUrl();
Logger.log(img);
}
希望这个翻译对您有所帮助。如果您有任何其他问题,可以随时向我提问。
英文:
Working code
function onFormSubmit(e) {
// Open the template presentation by ID
var templateDoc =
SlidesApp.openById('1bGKJ027bnUCgSpQrunXXBZCgFNXZHtF2ZnsaWHLh8B4');
// Create a copy of the template, we don't want to mess up the template
presentation
var newTempFile = DriveApp.getFileById(templateDoc.getId()).makeCopy();
// Open the new presentation for editing
var newSlide = SlidesApp.openById(newTempFile.getId());
// Get the responses triggered by on form submit
var items = e.response.getItemResponses();
// Find the text in the presentation and replace it with the form
response
// Items[0].getResponse() is the first response in the form and it is
the "Title"
// OpenSlide.replaceAllText('{Title}', items[0].getResponse());
// Items[1].getResponse() is the second and it is the date
// OpenSlide.replaceAllText('{Text}', items[1].getResponse());
// You can add as much as you have and change them in the template doc
like this
// OpenSlide.replaceAllText('{number}', items[2].getResponse());
// OpenSlide.replaceAllText('{choice}', items[3].getResponse());
// And so on...
var image = items[0].getResponse();
newSlide.getSlides().forEach(s => {
s.getShapes().forEach(e => {
if (e.getText().asString().trim() == '{{image1}}') {
e.replaceWithImage(DriveApp.getFileById(Array.isArray(image) ?
image[0] : image).getBlob());
}
})
});
// Save and close the new document
newSlide.saveAndClose();
// Save the new slide as a PDF
var newSlideFile = DriveApp.getFileById(newTempFile.getId());
var pdfBlob = newSlideFile.getAs('application/pdf');
var pdfName = items[0].getResponse() + '.pdf';
var folder =
DriveApp.getFolderById('1lEP4qtXs6fITXS9GKeaYKLfbRONgbuOQ');
folder.createFile(pdfBlob.setName(pdfName));
var folderId = "10XCnKfhk4_5agWCYxWHGCO6wwxWSG7Y9"
// Save the new slide as a PNG file
var slideId = newSlide.getSlides()[0].getObjectId();
var url = 'https://docs.google.com/presentation/d/' + newSlide.getId()
+ '/export/png?id=' + newSlide.getId() + '&pageid=' + slideId;
var options = {
headers: {
Authorization: 'Bearer ' + ScriptApp.getOAuthToken()
}
};
var response = UrlFetchApp.fetch(url, options);
var image = response.getAs(MimeType.PNG);
image.setName("png-"+Math.random().toFixed(2));
//image.setName(DriveApp.getFileById(presentationId).getName());
var img = DriveApp.getFolderById(folderId).createFile(image).getUrl()
Logger.log(img)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论