Convert all xls files available in a folder into "Google Doc Spreadsheets"? - google-apps-script

The basic way to do that is turn conversion on when uploading files into Google Drive.
Another way is to select the xls file in the folder and convert it one by one by hand.
But if one has already many xls files uploaded in a folder, it may be faster to convert them with Google Apps Script than re-uploading again the files.
In my case:
once converted, I need to delete the xls files
all xls files are below the limits : "Uploaded spreadsheet files that are converted to the Google spreadsheets format can’t be larger than 100 MB, and need to be under 400,000 cells and 256 columns per sheet."
https://support.google.com/drive/answer/37603?hl=en
Thanks in advance;)

you should use the Advanced Drive Service, file update
This service must be enabled before use but the doc is pretty clear.
EDIT (following your comments)
(sorry for the long delay, I forgot this post)
This code will convert every XLS files in your drive to Google spreadsheet format (as long as their names have an .xls extension)
You must authorize the Drive extended ressource + API console (follow the instructions from the ressource/advanced services menu, see illustration below)
function importXLS(){
var files = DriveApp.searchFiles('title contains ".xls"');// you can also use a folder as starting point and get the files in that folder... use only DriveApp method here.
while(files.hasNext()){
var xFile = files.next();
var name = xFile.getName();
if (name.indexOf('.xls')>-1){ // this check is not necessaey here because I get the files with a search but I left it in case you get the files differently...
var ID = xFile.getId();
var xBlob = xFile.getBlob();
var newFile = { title : name+'_converted',
key : ID
}
file = Drive.Files.insert(newFile, xBlob, {
convert: true
});
}
}
}

Related

How do I extract the text from a docx file using Apps Script?

The files are saved in a Drive folder. I need to send the text content of all .docx file as an API payload. I've tried using Blob but to no avail. Is there a way to get this done?
If I understand you correctly, you want to send the text content of a docx file that you have in Drive. If that's correct, then you can do the following:
function docx() {
var docxId ="your-docx-id";
var docx = DriveApp.getFileById(docxId);
var blob = docx.getBlob();
var file = Drive.Files.insert({}, blob, {convert:true});
var id = file["id"];
var doc = DocumentApp.openById(id);
var text = doc.getBody().getText();
return text;
}
This code uses Advanced Drive Service to create a Docs file out of the blob you get from the docx, via Drive.Files.insert. Then, you can easily access this newly created file via DocumentApp and use getText.
Bear in mind that this will create a new file every time you run it. Use Files.delete to avoid that.
I hope this is of any help.

Cannot use this operation on a shared drive item. (line8, file "Code")

I have been using this script on multiple shared drive to start cleaning up these orphan files. Below is the script. Issue I started seeing on two of the drives is this "Cannot use this operation on a shared drive item." Any ideas?
function collectOrphans(findOrphaned) {
var folder = DriveApp.getFoldersByName('Orphaned').next();
var files = DriveApp.getFiles();
var orphanCount = 0;
while(files.hasNext()){
var file = files.next();
if(!file.getParents().hasNext()){
folder.addFile(file);
orphanCount++;
}
}
Logger.log('Moved ' + orphanCount + ' orphans successfully.');
}"
This specific error is being thrown because you are trying to add a reference to a file which lays in a shared drive from another, different drive folder (your personal drive or a different shared drive).
In order to overcome this, you could:
Create an 'orphaned' folder for each of your drives, and repeat the function for each of them.
Use only one 'orphaned' folder, but use makeCopy() to copy the files to the folder rather than just creating a reference to each of them.
Additionally, in order to find orphaned files, you may want to check out using is:unorganized filter along with DriveApp.searchFiles() (see here).

Script - DriveApp.getFilesByName - get latest version?

As title suggests
Got a script that's fetching a CSV from a specific G-Drive folder, which is currently filtering by a specific file name.
Is it possible to just say 'get the latest file' from that folder instead of searching by the specific name?
Asking because multiple copies of the same file will be uploaded into the same folder (file name is content.csv, copies will either replace that file, or no doubt be uploaded as content(1).csv etc). So, therefore, assumed just trying to 'get the latest file' would be the best way?
Script:
function importCSVFromGoogleDrive() {
var fSource = DriveApp.getFolderById('xxxxxxxxxxxxx'); // folder where files are saved
var file = DriveApp.getFilesByName("content.csv").next(); // currently searching by file name
var csvData = Utilities.parseCsv(file.getBlob().getDataAsString()); csvData.splice(299,csvData.length-300);
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
}
You can order the files by date so you can list the recently uploaded files, and when you hit an already downloaded file you just stop whole process.
https://developers.google.com/drive/api/v3/reference/files/list#orderBy
Try this:
function getLatest(){
var optionalArgs = {
orderBy:"modifiedDate",
q:"title='TITLE'"
};
var files = Drive.Files.list(optionalArgs).items;
Logger.log(files);
}
This will use the Advanced Services API to search for the files and order them by date. To use this code you'll need to enable Google Advanced Services, which allows you to use the APIs directly. You should also look at the File Resource to know what properties are available. Lastly, files holds an array of the files returned by the list.

Google Apps Script returning all files in Drive, rather than files in a folder

I have a folder in Google Drive, where there are some files. I want to retrieve all of these file information, and that's all I want. There are many other files in other folders, but I don't need them. However, my following code is showing all files' names. How can I limit the result only to the files in the specified folder?
//Folder ID
var myFolderId = "XXXXXXXXXXXXXXXXXXXXXXXXX";
var files = DriveApp.getFolderById(myFolderId).getFiles();
while(files.hasNext())
{
var file = files.next();
Logger.log(file.getName());
}
For now, there are only 4 files in this folder, but the log shows the entire file names in my Google Drive. I don't know what I am doing wrong, because I am specifying the folder with ID. How come it is returning every file in Google Drive?
Answer:
Your code already works great!
Further information:
Google Apps Script doesn't allow the running of global code as specific functions need to be declared and run, at least, not in the same was as locally-run code. The global variables are static and can not be changed in the runtime.
Things you can do:
As Cooper mentioned in their comment - all you need to do is put your code in a function, call the function and you're set. The code works great:
function functionName(){
var myFolderId = "XXXXXXXXXXXXXXXXXXXXXXXXX";
var files = DriveApp.getFolderById(myFolderId).getFiles();
while(files.hasNext())
{
var file = files.next();
Logger.log(file.getName());
}
}
As a side-note: if you really want to use a global scope (or at least. use an emulation of it), the PropertiesService is available to you, though you'd still have to set them in a function and run a function to retrieve them:
function setVars(){
PropertiesService.getScriptProperties().setProperty('myFolderId', 'XXXXXXXXXXXXXXXXXXXXXXXXX');
}
function listFiles(){
var files = DriveApp.getFolderById(PropertiesService.getScriptProperties().getProperty('myFolderId')).getFiles();
//continue this code
}

Convert any xls-xlsm files to Google Sheet format and overwrite the oldest existing GS file with that name

I decided to open this topic because there is very little material in this regard and in my opinion many are asking the same question. I'm trying to write an script to convert an .xls or a .xlsm file stored on Google Drive to a Google Sheets file. I'm not interested in converting macros inside the .xlsm file, only converting the base file. This script should be able to overwrite the oldest file with the new one if it has the same name.
The topics in which I found more material are these:
How to automatically import data from uploaded CSV or XLS file into Google Sheets
Converting .xls to google spreadsheet in google apps script
Convert all xls files available in a folder into "Google Doc Spreadsheets"?
these topic links inserted above are partial and in any case they do not allow the conversion of the .xlsm format.
I started from this code (taken from here), but I don't know how to modify it to make it convert both the .xls and .xlsm files and let them overwrite them if they are already in Google Drive
/**
* Sample use of convertExcel2Sheets() for testing
**/
function testConvertExcel2Sheets() {
var xlsId = "0B9**************OFE"; // ID of Excel file to convert
var xlsFile = DriveApp.getFileById(xlsId); // File instance of Excel file
var xlsBlob = xlsFile.getBlob(); // Blob source of Excel file for conversion
var xlsFilename = xlsFile.getName(); // File name to give to converted file; defaults to same as source file
var destFolders = []; // array of IDs of Drive folders to put converted file in; empty array = root folder
var ss = convertExcel2Sheets(xlsBlob, xlsFilename, destFolders);
}
I would like to thank anyone who decided to help me and therefore to give all those who are asking the same question.