I am trying to convert a .pdf file to text using Apps Script called from a Google Sheet. I am using the Advanced Drive API service (it seems the only way to do it). I have added the Drive API service and deployed the app as an API. The app runs, uploads the pdf from users pc to a drive file. The file is passed to code copied from Github which should perform the conversion but fails with
TypeError: Drive.Files.insert is not a function
I don't think this is the code in error but maybe how I have configured the Apps Script. I am struggling to find any documentation that can help me see where I have gone wrong.
The function which fails in line 17 const { id, title } = Drive.Files.insert(
:
/*
* Convert PDF file to text
* @param {string} fileId - The Google Drive ID of the PDF
* @param {string} language - The language of the PDF text to use for OCR
* return {string} - The extracted text of the PDF file
*/
const convertPDFToText = (fileId, language) => {
fileId = fileId || '18FaqtRcgCozTi0IyQFQbIvdgqaO_UpjW'; // Sample PDF file
language = language || 'en'; // English
// Read the PDF file in Google Drive
const pdfDocument = DriveApp.getFileById(fileId);
// Use OCR to convert PDF to a temporary Google Document
// Restrict the response to include file Id and Title fields only
const { id, title } = Drive.Files.insert(
{
title: pdfDocument.getName().replace(/\.pdf$/, ''),
mimeType: pdfDocument.getMimeType() || 'application/pdf',
},
pdfDocument.getBlob(),
{
ocr: true,
ocrLanguage: language,
fields: 'id,title',
}
);
// Use the Document API to extract text from the Google Document
const textContent = DocumentApp.openById(id).getBody().getText();
// Delete the temporary Google Document since it is no longer needed
DriveApp.getFileById(id).setTrashed(true);
// (optional) Save the text content to another text file in Google Drive
const textFile = DriveApp.createFile(`${title}.txt`, textContent, 'text/plain');
return textContent;
};
About your error message of TypeError: Drive.Files.insert is not a function
, on December 11, 2023, Drive API of Advanced Google services could use v3. Ref And, now, when Drive API is enabled at Advanced Google services, Drive API v3 is used as the default version. And, in Drive API v3, insert
method is not included in Drive.Files
. I guessed that this might be the reason for your current issue.
When I saw your showing script, it seemed that it was for Drive API v2. So, if you want to use your showing script, please set Drive API v3 to v2 in Advanced Google services as follows.
or
And, as another approach, if your showing script is converted to Drive API v3, it becomes as follows.
/*
* Convert PDF file to text
* @param {string} fileId - The Google Drive ID of the PDF
* @param {string} language - The language of the PDF text to use for OCR
* return {string} - The extracted text of the PDF file
*/
const convertPDFToText = (fileId, language) => {
fileId = fileId || '18FaqtRcgCozTi0IyQFQbIvdgqaO_UpjW'; // Sample PDF file
language = language || 'en'; // English
// Read the PDF file in Google Drive
const pdfDocument = DriveApp.getFileById(fileId);
// Use OCR to convert PDF to a temporary Google Document
// Restrict the response to include file Id and Title fields only
const { id, name } = Drive.Files.create(
{
name: pdfDocument.getName().replace(/\.pdf$/, ''),
mimeType: MimeType.GOOGLE_DOCS,
},
pdfDocument.getBlob(),
{
ocrLanguage: language,
fields: 'id,name',
}
);
// Use the Document API to extract text from the Google Document
const textContent = DocumentApp.openById(id).getBody().getText();
// Delete the temporary Google Document since it is no longer needed
DriveApp.getFileById(id).setTrashed(true);
// (optional) Save the text content to another text file in Google Drive
const textFile = DriveApp.createFile(`${name}.txt`, textContent, 'text/plain');
return textContent;
};
This modified script works with Drive API v3.
oauthScopes
from appsscript.json
and run the script again. If an error is not removed, please create a new Google Apps Script project and copy and paste the script and enable Drive API v2 or v3 and test it again.