我正在尝试编写一个附加到 Google 表单的脚本。该表单收集用户的姓名和电子邮件,然后将其姓名放在 Google Slide 上的证书上,并将其以 PDF 形式保存在 Google Drive 中。我希望程序发送一封包含 PDF 或 PDF 链接的电子邮件。
不过我遇到了权限错误。我可以让它做除发送电子邮件之外的所有事情。该表格将存放在 Google 网站上。
function onFormSubmit(e) {
// 1. Get form responses
var formResponse = e.response;
var itemResponses = formResponse.getItemResponses();
// First item is the user's name and the second item is the user's email
var userName = itemResponses[0].getResponse();
var userEmail = itemResponses[1].getResponse();
// 2. Open the Google Slides presentation. Presentation is shared to anyone with the link.
var presentationUrl = MY SHARED URL GOES HERE;
var presentationId = presentationUrl.match(/\/presentation\/d\/([a-zA-Z0-9-_]+)/)[1];
var presentation = SlidesApp.openById(presentationId);
// 3. Replace placeholder text with user's name
var slides = presentation.getSlides();
var shapeIndex = 3; // Index of the shape containing the placeholder text
var slide = slides[0];
var shape = slide.getShapes()[shapeIndex];
if (shape) {
shape.getText().setText(userName);
// Save changes made to the presentation
presentation.saveAndClose();
// Wait for changes to be applied
Utilities.sleep(5000); // 5 seconds delay
// 4. Export presentation as PDF. This is where I keep getting the error message.
var pdf = DriveApp.getFileById(presentationId).getAs('application/pdf');
// Extracting folder ID from the provided folder URL. The folder is shared to anyone with the link.
var folderUrl = URL TO FOLDER GOES HERE.;
var folderId = folderUrl.match(/[-\w]{25,}/);
if (folderId) {
folderId = folderId[0];
} else {
throw new Error("Folder ID not found in the URL.");
}
// Accessing the folder and saving the PDF file.
var folder = DriveApp.getFolderById(folderId);
//THIS IS WHERE I KEEP GETTING THE ERROR MESSAGE
var pdfFile = folder.createFile(pdf);
var pdfFileName = userName + "_Certificate.pdf";
pdfFile.setName(pdfFileName);
// Generating the URL for the file in the user's local Google Drive
var fileUrl = FILE URL GOES HERE;
// 5. Send email to the user with the download link
var subject = "Your Certificate";
var body = "Dear " + userName + ",\n\nPlease find your certificate attached or download it from the following link:\n" + fileUrl;
MailApp.sendEmail(userEmail, subject, body, { attachments: [pdfFile] });
} else {
Logger.log("Shape not found.");
}
}
想法?
var userName = 'Name'; var userEmail = 'Email';
我建议仔细检查您的权限:
关于最后一点,您说“该文件夹已共享给任何有链接的人” - 但他们有编辑权限吗?无论如何,您可能不希望这样,因为这会给世界完全访问权限,但只要表单(或脚本,如果未绑定)的所有者具有编辑器访问权限,它就应该可以工作。
我复制了你的脚本(根据个人喜好对流程进行了一些调整),它在我的 Google 工作区中运行良好。我复制了下面稍微编辑过的脚本,以防它有用 - 我怀疑我所做的非常小的更改对您来说是问题。
function onFormSubmit(e) {
// 1. Get form responses
var formResponse = e.response;
var itemResponses = formResponse.getItemResponses();
// First item is the user's name and the second item is the user's email
var userName = itemResponses[0].getResponse();
var userEmail = itemResponses[1].getResponse();
Logger.log(userName + ", " + userEmail);
// 2. Open the Google Slides presentation. Presentation is shared to anyone with the link.
var presentationUrl = "https://docs.google.com/presentation/d/1B-A0x2OtQmlYAe1Yh9YvBh90foZSs9JMKHfrii2XXPc/edit#slide=id.p";
var presentationId = presentationUrl.match(/[-\w]{25,}/);
Logger.log(presentationId);
var presentation = SlidesApp.openById(presentationId);
// 3. Replace placeholder text with user's name
var slides = presentation.getSlides();
var shapeIndex = 3; // Index of the shape containing the placeholder text
var slide = slides[0];
var shape = slide.getShapes()[shapeIndex];
if (shape) {
shape.getText().setText(userName);
// Save changes made to the presentation
presentation.saveAndClose();
//4a. Get the folder where you want to save the PDF first
// Extracting folder ID from the provided folder URL. The folder is shared to anyone with the link.
var folderUrl = "https://drive.google.com/drive/folders/1Pg6QJZX9WACTCwh0uY6nFRymNg695B1K";
var folderId = folderUrl.match(/[-\w]{25,}/);
if (folderId) {
folderId = folderId[0];
} else {
throw new Error("Folder ID not found in the URL.");
}
var folder = DriveApp.getFolderById(folderId);
// 4b. THEN Export presentation as PDF to the folder.
var pdf = DriveApp.getFileById(presentationId).getAs('application/pdf');
pdf.setName(userName + "_Certificate.pdf");
var pdfFile = folder.createFile(pdf);
var fileUrl = pdfFile.getUrl();
// 5. Send email to the user with the download link
var subject = "Your Certificate";
var body = "Dear " + userName + ",\n\nPlease find your certificate attached or download it from the following link:\n" + fileUrl;
MailApp.sendEmail(userEmail, subject, body, { attachments: [pdfFile] });
} else {
Logger.log("Shape not found.");
}
}