I am working to automate a process. Currently, I am uploading Excel (.xlsx
) files to a specific folder in Drive. I then manually convert them into a Google Sheet file, and move it into a separate folder. I would like to automate this process. I know I would have to write the script to iterate through the 2 folders to compare and see which files have yet to be converted, and then convert those it does not find in both locations. However, I am struggling with the best approach to make this happen.
The code below that may or may not be on the right track, I am rather new at this and really just tried piecing it together. Anyone's insight would be greatly appreciated.
function Excel2Sheets()
{
//Logs excel folder and string of files within
var excelfolder = DriveApp.getFolderById('1JbamZxNhAyZT3OifrIstZKyFF_d257mq');
var excelfiles = excelfolder.getFiles();
// Logs sheets folder and string of files within
var sheetfolder = DriveApp.getFolderById('1y10IwMobCdpQlYwWdveHLzxEz3Xml0Qt');
var ID = sheetfolder.getId();
var sheetfiles = sheetfolder.getFiles();
var MType = MimeType.GOOGLE_SHEETS;
while (excelfiles.hasNext()) {
var excelfile = excelfiles.next();
var excelname = excelfile.getName();
while (sheetfiles.hasNext()) {
var sheetfile = sheetfiles.next();
var sheetname = sheetfile.getName();
if(sheetname == excelname) {
break;
}
if(sheetfiles.hasNext(0)) {
var blob = excelfile.getBlob();
sheetfolder.createFile(excelname, blob, MType);
break;
}
}
}
}
I have also played around with this code. Thanks
function fileChecker()
{
try{
//Establishes Excel Source Folder
var excelfolder = DriveApp.getFolderById('1JbamZxNhAyZT3OifrIstZKyFF_d257mq');
//Establishes Sheet Target Folder
var sheetfolder = DriveApp.getFolderById('1y10IwMobCdpQlYwWdveHLzxEz3Xml0Qt');
//Establishes Return File Type
var MType = MimeType.GOOGLE_SHEETS;
//Gets all files in excel folder
var excelfiles = excelfolder.getFiles();
//loop through excel files
while(excelfiles.hasNext()){
//Establishes specific excel file
var excelfile = excelfiles.next();
//Checks for file with same name in sheets folder
var sheetfiles = sheetfolder.getFilesByName(excelfile.getName());
//Logical Test for file match
if(sheetfiles.hasNext()){
//Gets File Name
var excelname = excelfile.getName();
//Creates File Blob
var blob = excelfile.getBlob();
// Creates sheet file with given name and data of excel file
sheetfolder.createFile(excelname, blob, MType);
}
}
}
catch(err){
Logger.log(err.lineNumber + ' - ' + err);
}
}
Either of your codes, if they were to reach the
createFile
line, should throw this error:because you are passing a
Blob
whilecreateFile(name, content, mimetype)
expects aString
.Reviewing the reference page for DriveApp, one will undoubtedly notice the
File#getAs(mimetype)
method, which returns Blob, and theFolder#createFile(blob)
methods, and try something like:This too, however, will return an error:
Looking at the documentation for the
getAs
method indicates that this is, in general, an unsupported operation unless the destination mimetype isMimeType.PDF
. My guess is this is because the PDF conversion is simple enough - its implementation likely uses a "Print"-like functionality - while spreadsheet format conversion requires careful handling of formulas, images, charts, etc.From past experiences with Google Drive, the general user knows that the ability to use Drive to perform automatic conversion of Excel -> Google Sheets exists. However, this functionality is only available during upload. Drawing from a closely related question, we observe that we must use the Drive API "Advanced Service", rather than the simpler, native
DriveApp
. Enable the Advanced Service, and then the following snippet can work. Note that the Advanced Drive Service treats folders as files having a specific mimetype (which is why there are no folder-specific methods), so using both DriveApp and the Advanced Service is easiest for those in the Apps Script environment.My code above enforces a somewhat-reasonable requirement that the files you care about are those that you own. If that's not the case, then removal of the email check is advised. Just beware of converting too many spreadsheets in a given day.
If working with the Advanced Service in Apps Script, it can often be helpful to review Google's API Client Library documentation for the associated API since there is no specific Apps Script documentation equivalent. I personally find the Python equivalent easiest to work with.