The company I work for just updated its branding, affecting hundreds of Excel and PowerPoint files saved to a Shared Google Drive. There is a team working to update the files. My challenge is finding an efficient way to add the new files and delete the old files in Google Drive.
I created a Google Sheet to capture the current File Id, the new File Id, and the Folder Id, along with the following Google Apps Script code. The code throws the following error.
Exception: Unexpected error while getting the method or property getFileById on object DriveApp.
BulkUpdate @ Bulk Update.gs:25
Hoping someone can help me debug? I'm a novice and I've hit a wall.
function BulkUpdate() {
// This constant is added to updated files
var success = "Success"
// Define range
var sheet = SpreadsheetApp.getActiveSheet();
var firstRow = 2; // First row of data to process
var lastRow = sheet.getLastRow(); // Last row of data to process
// Fetch range
var dataRange = sheet.getRange(firstRow, 1, lastRow, 4);
// Fetch values for each row in range
var data = dataRange.getValues();
for (var i = 0; data.length; ++i) {
var row = data[i];
var folderId = row[0]; // First column
var currentFileId = row[1]; // Second column
var newFileId = row[2]; // Third column
var confirmation = row[3]; // Fourth column
// Copy new file to folder and trash current file
if (confirmation !== success) {
var currentFile = DriveApp.getFileById(currentFileId);
var newFile = DriveApp.getFileByID(newFileId);
var folder = DriveApp.getFolderById(folderId);
newFile.makeCopy(folder);
currentFile.setTrashed(true);
// Update spreadsheet
sheet.getRange(firstRow + i, 4).setValue(success);
SpreadsheetApp.flush();
}
}
// Create a popup window
SpreadsheetApp.getUi().alert("Updates Complete");
}