I have a google sheet posting materials assignments to Google classroom. These include 'evidence' image and pdf attachments. When the material is posted the attachments are linked as HTML. The code seems to be converting the image files (jpg) to HTML.
This is the upload function. I tried adding in different file extensions but this made no difference.
function uploadFileToDrive(url) {
var response = UrlFetchApp.fetch(url, {
headers: {
Authorization: 'Bearer ' + ScriptApp.getOAuthToken()
}
});
var contentType = response.getHeaders()['Content-Type'];
var fileExtension = url.substring(url.lastIndexOf('.') + 1);
var mimeType;
switch (fileExtension) {
case 'jpg':
mimeType = 'image/jpg';
break;
case 'pdf':
mimeType = 'application/pdf';
break;
// Add more cases for other file types as needed
default:
mimeType = contentType;
break;
}
var file = Drive.Files.insert({
title: "evidence",
mimeType: mimeType
}, response.getBlob());
return file.id;
}
As just my guess, from your previous question, if you are using https://drive.google.com/open?id=###spreadsheetId###
as the value of url
, in this case, I think that the created file using your showing script is an HTML data of the login screen. I thought that this might be the reason for your current issue.
If you want to export Google Spreadsheet as a PDF file, how about the following modification?
In this case, please use the file ID. If you are using a URL like https://drive.google.com/open?id=###spreadsheetId###
, ###spreadsheetId###
is the file ID.
function uploadFileToDrive(fileId) {
var blob = DriveApp.getFileById(fileId).getBlob();
var file = Drive.Files.insert({ title: "evidence" }, blob);
return file.id;
}
From your following reply,
running this gives Unexpected error while getting the method or property getFileById on object DriveApp. uploadFileToDrive @ var blob = DriveApp.getFileById(fileId).getBlob(); createMaterialsFromGoogleSheet @ evidenceId = uploadFileToDrive(evidenceLink);
Is this a step forward? function uploadFileToDrive(fileId) { var blob = Drive.Files.get(fileId).getBlob(); var fileMetadata = { title: file.title }; var uploadedFile = Drive.Files.insert(fileMetadata, blob); return uploadedFile.id; } gives this error - GoogleJsonResponseException: API call to drive.files.get failed with error: File not found: drive.google.com/open?id=18exMxxxxxxxxxNbbMAqVnJOxxxxxxxxx - I am the owner of the file and can view it in a browser.
In my initial answer, I have mentioned In this case, please use the file ID. If you are using a URL like https://drive.google.com/open?id=###spreadsheetId###, ###spreadsheetId### is the file ID.
. But, from your reply, if my understanding is correct, unfortunately, I'm worried that you have still used an URL. Because, when the invalid file ID is given to my script, the same error occurs. If my understanding is correct, please use the file ID instead of the URL, and test it again.
If you are required to use a URL like https://drive.google.com/open?id=###spreadsheetId###
, please modify the above script as follows.
function uploadFileToDrive(url) {
var fileId = url.split("=")[1].split("&")[0];
var blob = DriveApp.getFileById(fileId).getBlob();
var file = Drive.Files.insert({ title: "evidence" }, blob);
return file.id;
}
But, if the URL is different from https://drive.google.com/open?id=###spreadsheetId###
, an error occurs. Please be careful about this.
And, if you directly use the file ID, please use my first suggested script. Please be careful about this.