Reputation: 79
These two functions work alone. I wanted to write them down now, but unfortunately it doesn't work. Does anyone have a hint?
I want the "pdfToDoc" function to work with my array list (var Files [])
var files = [];
function getListOfId(){
var folderId = "11tjb_odTJ2E_eXXXXXXXDViQQJ8u9-g";
var filesN = DriveApp.getFolderById(folderId).getFiles();
while (filesN.hasNext()) files.push(filesN.next().getId());
//console.log(files);
}
function pdfToDoc() {
var fileBlob = DriveApp.getFileById('16wkcb8XXXXXXXXmpQ19_yM-K2QxxIc').getBlob();
var resource = {
title: fileBlob.getName(),
mimeType: fileBlob.getContentType()
};
var options = {
ocr: true,
convert: true
};
var docFile = Drive.Files.insert(resource, fileBlob, options);
//Logger.log(docFile.alternateLink);
}
function pdfToDoc() {
var files = [];
function getListOfId(){
var folderId = "11tjb_odTJ2E_XXXXXXX9DViQQJ8u9-g";
var filesN = DriveApp.getFolderById(folderId).getFiles();
while (filesN.hasNext()) files.push(filesN.next().getId());
}
for(var i=0;i<files.lenght;i++){
var fileBlob = DriveApp.getFileById(files[i][0]).getBlob();
var resource = {
title: fileBlob.getName(),
mimeType: fileBlob.getContentType()
};
var options = {
ocr: true,
convert: true
};
var docFile = Drive.Files.insert(resource, fileBlob, options);
var docFileId = docFile.getId();
var saveDocFile = DriveApp.getFileById(docFileId);
DriveApp.getFolderById("1-k_0SXXXXXXXXXXXXXXXXXXXXXAyX").addFile(saveDocFile);
DriveApp.getRootFolder().removeFile(saveDocFile);
}}
Upvotes: 1
Views: 177
Reputation: 188
What I see that you're putting function inside a function instead you should call the function in that function.
Try this:
function pdfToDoc() {
var files = [];
getListOfId(files);
for(var i=0;i<files.lenght;i++){
var fileBlob = DriveApp.getFileById(files[i][0]).getBlob();
var resource = {
title: fileBlob.getName(),
mimeType: fileBlob.getContentType()
};
var options = {
ocr: true,
convert: true
};
var docFile = Drive.Files.insert(resource, fileBlob, options);
var docFileId = docFile.getId();
var saveDocFile = DriveApp.getFileById(docFileId);
DriveApp.getFolderById("1-k_0SXXXXXXXXXXXXXXXXXXXXXAyX").addFile(saveDocFile);
DriveApp.getRootFolder().removeFile(saveDocFile);
}}
function getListOfId(files){
var folderId = "11tjb_odTJ2E_XXXXXXX9DViQQJ8u9-g";
var filesN = DriveApp.getFolderById(folderId).getFiles();
while (filesN.hasNext()) files.push(filesN.next().getId());
}
Upvotes: 0
Reputation: 1221
I tried replicating your code and it seems that the problem lies with your function getListOfId() which wasn't called anywhere in the code. To fix this, you should add a function call before you loop into the file ids.
This is how I made it work on my end and the code looks like this(May not be exactly how you want it; I also fixed the typo? lenght to length):
function pdfToDoc() {
var files = [];
function getListOfId(){
var folderId = "1BpPZynsF5tjI5bysoOeHjKbr3gFCm7P8";
var filesN = DriveApp.getFolderById(folderId).getFiles();
while (filesN.hasNext()) files.push(filesN.next().getId());
}
getListOfId()
for(var i=0;i<files.length;i++){
var fileBlob = DriveApp.getFileById(files[i]).getBlob();
var resource = {
title: fileBlob.getName(),
mimeType: fileBlob.getContentType()
};
var options = {
ocr: true,
convert: true
};
var docFile = Drive.Files.insert(resource, fileBlob, options);
var docFileId = docFile.getId();
var saveDocFile = DriveApp.getFileById(docFileId);
DriveApp.getFolderById("1BpPZynsF5tjI5bysoOeHjKbr3gFCm7P8").addFile(saveDocFile);
DriveApp.getRootFolder().removeFile(saveDocFile);
}
}
You can change the code on your own preference on how you will be calling the getListOfId() function.
Upvotes: 1
Reputation: 38425
pdfToDoc
function should use a parameter / argument for the file id instead of a hardcoded idpdfToDoc
Here is one simple way to implement the above
var files = [];
function getListOfId(){
var folderId = "11tjb_odTJ2E_eXXXXXXXDViQQJ8u9-g";
var filesN = DriveApp.getFolderById(folderId).getFiles();
while (filesN.hasNext()) files.push(filesN.next().getId());
//console.log(files);
}
function pdfToDoc(id) { // modified
var fileBlob = DriveApp.getFileById(id).getBlob(); // modified
var resource = {
title: fileBlob.getName(),
mimeType: fileBlob.getContentType()
};
var options = {
ocr: true,
convert: true
};
var docFile = Drive.Files.insert(resource, fileBlob, options);
//Logger.log(docFile.alternateLink);
}
/**
* Added
*/
function main(){
getListOfId();
// files is a global variable
files.forEach(function(id){
pdfToDoc(id);
});
}
Additional note:
Using global variables might be tricky. I suggest you to move your files
variable initialization to the "main" function, even better, make that your getListOfId
function returns the collected ids instead of modifying a global variable. Tip: Learn the basics of functional programming.
Upvotes: 0