流程概述 我正在使用 Google 表格来记录有关共享 Google 云端硬盘文件夹中的文件的信息。目标是记录文件 ID、标题、URL、提交日期和时间以及创建者的电子邮件地址等详细信息。但是,我面临一个问题,即在使用共享云端硬盘时无法检索创建文档的用户的电子邮件。
遇到的问题 尽管脚本可以记录文件详细信息,但它无法检索在共享驱动器中创建文档的用户的电子邮件。遇到的错误是:
请求帮助 我正在寻求有关如何正确检索在共享云端硬盘中创建文档的用户的电子邮件的帮助。此外,我计划使用 AppSheet 创建一个应用程序来批准和理解文档,旨在为教师提供帮助。还有其他方法可以实现此功能吗?任何指导或替代方法将不胜感激。
`function checkNewFiles() {
var folderId = PropertiesService.getScriptProperties().getProperty('FOLDER_ID'); // Substitua pelo ID da sua pasta
var sheetId = PropertiesService.getScriptProperties().getProperty('SHEET_ID'); // Substitua pelo ID da sua planilha
var folder = DriveApp.getFolderById(folderId);
var sheet = SpreadsheetApp.openById(sheetId).getSheets()[0];
var files = folder.getFiles();
var newFiles = [];
while (files.hasNext()) {
var file = files.next();
var createdDate = file.getDateCreated();
var createdDay = Utilities.formatDate(createdDate, Session.getScriptTimeZone(), 'yyyy-MM-dd');
var createdTime = Utilities.formatDate(createdDate, Session.getScriptTimeZone(), 'HH:mm:ss');
Logger.log('Checking file: ' + file.getName() + ' created on ' + createdDate);
// Verificar se o arquivo já está na planilha
if (!isFileInSheet(sheet, file.getId())) {
Logger.log('New file found: ' + file.getName());
var creatorName = getFileOrganizerName(file.getId()); // Obter o nome do criador (fileOrganizer)
newFiles.push([
file.getId(),
file.getName(),
file.getUrl(),
createdDay,
createdTime,
creatorName,
'',
'',
'',
'',
]);
}
}
if (newFiles.length > 0) {
Logger.log('New files to be added: ' + newFiles.length);
sheet.getRange(sheet.getLastRow() + 1, 1, newFiles.length, newFiles[0].length).setValues(newFiles);
} else {
Logger.log('No new files found.');
}
}
// Função atualizada para obter o nome do fileOrganizer usando a API avançada do Google Drive
function getFileOrganizerName(fileId) {
try {
var file = Drive.Files.get(fileId, {fields: 'permissions'});
if (file.permissions && file.permissions.length > 0) {
for (var i = 0; i < file.permissions.length; i++) {
if (file.permissions[i].role === 'organizer' || file.permissions[i].role === 'fileOrganizer') {
return file.permissions[i].displayName;
}
}
}
return 'N/A';
} catch (e) {
Logger.log('Unable to get organizer name for file: ' + fileId + ' - ' + e.message);
return 'N/A';
}
}
function isFileInSheet(sheet, fileId) {
var data = sheet.getDataRange().getValues();
for (var i = 0; i < data.length; i++) {
if (data[i][0] === fileId) {
return true;
}
}
return false;
}
function setupSheet() {
var sheetId = PropertiesService.getScriptProperties().getProperty('SHEET_ID'); // Substitua pelo ID da sua planilha
var sheet = SpreadsheetApp.openById(sheetId).getSheets()[0];
var headers = ['ID', 'Title', 'URL', 'Submission Date', 'Submission Time', 'Teacher Name', 'Segment', 'Class', 'Approval Status', 'Approval Date', 'Comments'];
sheet.getRange('A1:K1').setValues([headers]);
}
`
function sendReminders() {
try {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName("Q2'24 PBC Listing (Master)");
if (!sheet) throw new Error("Sheet not found");
var sheetUrl = spreadsheet.getUrl();
var quarter = sheet.getRange("C1").getValue();
var dataRange = sheet.getRange("A9:L").getValues(); // Adjusted to start from row 9
var today = new Date();
var itemNumberIndex = 0; // PBC Item Number column (A)
var applicabilityIndex = 1; // Applicability column (B)
var statusIndex = 2; // Status column (C)
var providerIndex = 4; // PBC Provider column (E)
var requesterIndex = 5; // PBC Requester column (F)
var sectionDescIndex = 7; // Section Desc column (H)
var entityIndex = 8; // Entity column (I)
var pbcDescIndex = 9; // PBC Desc column (J)
var asOfDateIndex = 10; // As Of Date column (K)
var dueDateIndex = 11; // Due Date column (L)
dataRange.forEach(function(row, index) {
var itemNumber = row[itemNumberIndex];
var applicability = row[applicabilityIndex];
var status = row[statusIndex];
var providers = extractEmails(row[providerIndex]);
var requesters = extractEmails(row[requesterIndex]);
var sectionDesc = row[sectionDescIndex];
var entity = row[entityIndex];
var pbcDesc = row[pbcDescIndex];
var asOfDate = new Date(row[asOfDateIndex]);
var dueDate = new Date(row[dueDateIndex]);
if ((quarter === "q4" && applicability === "10K") || applicability === "10Q / 10K") {
var daysRemaining = Math.ceil((dueDate - today) / (1000 * 60 * 60 * 24));
if (status === "Open") {
var subjectBase = "Reminder: TAFR PBC Items - due in ";
var subject;
var body;
if (daysRemaining < 0) {
subject = "Reminder: TAFR PBC Items - overdue";
body = generateEmailBody(itemNumber, sectionDesc, entity, pbcDesc, asOfDate, requesters, sheetUrl, true, dueDate);
} else {
subject = subjectBase + daysRemaining + " days";
body = generateEmailBody(itemNumber, sectionDesc, entity, pbcDesc, asOfDate, requesters, sheetUrl, false, dueDate);
}
if (daysRemaining === 7 || daysRemaining === 3 || daysRemaining === 1 || daysRemaining < 0) {
sendEmails(providers, subject, body);
}
}
}
});
} catch (error) {
Logger.log("Error: " + error.message);
}
}
function extractEmails(taggedEmails) {
return taggedEmails.split(",").map(function(email) {
var emailPattern = /<(.+?)>/;
var match = email.match(emailPattern);
return match ? match[1].trim() : email.trim();
});
}
function generateEmailBody(itemNumber, sectionDesc, entity, pbcDesc, asOfDate, requesters, sheetUrl, isOverdue, dueDate) {
var body = isOverdue ?
"The document below is overdue. Please upload it into the box folder as soon as possible, with the PBC item number in the name.\n\n" :
"The document below is due on " + dueDate.toDateString() + ". Please upload it into the box folder as soon as possible, with the PBC item number in the name.\n\n";
body += "PBC Item #" + itemNumber + "\n\n";
body += "Section Desc: " + sectionDesc + "\n\n";
body += "Entity: " + entity + "\n\n";
body += "PBC Desc: " + pbcDesc + "\n\n";
body += "As Of Date: " + asOfDate.toDateString() + "\n\n";
body += "Requesters: " + requesters.join(", ") + "\n\n";
body += "PBC Sheet Link: " + sheetUrl + "\n\n";
body += "Please indicate that you have uploaded the document in the TAFR PBC listing by selecting 'Uploaded' in the drop-down menu. Reminder emails will continue until this PBC item is marked as 'Uploaded' in the sheet.";
return body;
}
function sendEmails(recipients, subject, body) {
recipients.forEach(function(email) {
try {
MailApp.sendEmail(email, subject, body);
} catch (error) {
Logger.log("Error sending email to " + email + ": " + error.message);
}
});
}
function createTrigger() {
ScriptApp.newTrigger('sendReminders')
.timeBased()
.atHour(11)
.inTimezone("America/Los_Angeles")
.everyDays(1)
.create();
}