I see that the Drive Folder
class has a createFile() which takes three arguments for name, content, and mimeType. Is it possible to use this as part of an upload call to have a user file uploaded and converted (to Google Docs) all in one go without having to call the REST API directly with convert=true
?
For example, here's the HTML:
<html>
<head>
<base target="_top">
<script>
function handler(response) {
document.getElementById('uploader').innerHTML = "Uploaded file! " + response;
}
</script>
</head>
<body>
<div id="uploader">
<form>
<input type="file" name="theFile">
<input type="button" onclick="google.script.run.withSuccessHandler(handler).uploadFile(this.parentNode)" value="Upload!">
</form></div>
</body>
</html>
And here's the Google Script code:
function uploadFile(e) {
Logger.log("Uploading file!");
var dfolder = DriveApp.getFolderById('abcdefgh'); // replace w/ Drive FolderID
return dfolder.createFile(e.theFile).getName();
}
How would I go about changing that last line to something like:
return dfolder.createFile(newName, e.theFile, MimeType.GOOGLE_SHEETS);
Something I'm trying to figure out now is simply how to get the name of the file being uploaded (e.g. for newName
). And then how to go about converting whatever form e.theFile
is in to a string. If I try this as-is now I get the error:
Invalid argument: file.contentType at uploadFile(Code:31)