function doPost(e) {
var data = Utilities.base64Decode(e.parameters.data);
var blob = Utilities.newBlob(data, e.parameters.mimetype, e.parameters.filename);
var file =DriveApp.createFile(blob);
convertExceltoGoogleSpreadsheet(e.parameters.filename);
var output = HtmlService.createHtmlOutput( JSON.stringify(file) +"<b>Done!</b>");
output.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
return output;
// return ContentService.createTextOutput("Done.") <--- Here, an error occurred.
}
function convertExceltoGoogleSpreadsheet(fileName) {
try {
fileName = fileName || "microsoft-excel.xlsx";
var excelFile = DriveApp.getFilesByName(fileName).next();
var fileId = excelFile.getId();
var folderId = Drive.Files.get(fileId).parents[0].id;
var blob = excelFile.getBlob();
var resource = {
title: excelFile.getName(),
mimeType: MimeType.GOOGLE_SHEETS,
parents: [{id: folderId}],
};
file = Drive.Files.insert(resource, blob, {"convert": true });
} catch (f) {
Logger.log(f.toString());
}
}
<!DOCTYPE html>
<html>
<head>
<title>Sample script for uploading file to Google Drive without authorization</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.js"></script>
</head>
<body>
<form action="https://script.google.com/macros/s/##/exec" id="form" method="post">
Upload a file
<div id="data"></div>
<input name="file" id="uploadfile" type="file">
<input id="submit" type="submit">
</form>
<script>
$('#uploadfile').on("change", function() {
var file = this.files[0];
var fr = new FileReader();
fr.fileName = file.name;
fr.onload = function(e) {
e.target.result
html = '<input type="hidden" name="data" value="' + e.target.result.replace(/^.*,/, '') + '" >';
html += '<input type="hidden" name="mimetype" value="' + e.target.result.match(/^.*(?=;)/)[0] + '" >';
html += '<input type="hidden" name="filename" value="' + e.target.fileName + '" >';
$("#data").empty().append(html);
}
debugger;
fr.readAsDataURL(file);
});
</script>
</body>
</html>
Note: This will require enabling advanced Drive service from within Google Apps Script - Menu > Resources > Advanced Google Services AND Menu > Resources > Advanced Google Services > Drive API and Admin Directory API
var data = Utilities.base64Decode(e.parameters.data);
var blob = Utilities.newBlob(data, e.parameters.mimetype, e.parameters.filename);
var file =DriveApp.createFile(blob);
convertExceltoGoogleSpreadsheet(e.parameters.filename);
var output = HtmlService.createHtmlOutput( JSON.stringify(file) +"<b>Done!</b>");
output.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
return output;
// return ContentService.createTextOutput("Done.") <--- Here, an error occurred.
}
function convertExceltoGoogleSpreadsheet(fileName) {
try {
fileName = fileName || "microsoft-excel.xlsx";
var excelFile = DriveApp.getFilesByName(fileName).next();
var fileId = excelFile.getId();
var folderId = Drive.Files.get(fileId).parents[0].id;
var blob = excelFile.getBlob();
var resource = {
title: excelFile.getName(),
mimeType: MimeType.GOOGLE_SHEETS,
parents: [{id: folderId}],
};
file = Drive.Files.insert(resource, blob, {"convert": true });
} catch (f) {
Logger.log(f.toString());
}
}
<!DOCTYPE html>
<html>
<head>
<title>Sample script for uploading file to Google Drive without authorization</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.js"></script>
</head>
<body>
<form action="https://script.google.com/macros/s/##/exec" id="form" method="post">
Upload a file
<div id="data"></div>
<input name="file" id="uploadfile" type="file">
<input id="submit" type="submit">
</form>
<script>
$('#uploadfile').on("change", function() {
var file = this.files[0];
var fr = new FileReader();
fr.fileName = file.name;
fr.onload = function(e) {
e.target.result
html = '<input type="hidden" name="data" value="' + e.target.result.replace(/^.*,/, '') + '" >';
html += '<input type="hidden" name="mimetype" value="' + e.target.result.match(/^.*(?=;)/)[0] + '" >';
html += '<input type="hidden" name="filename" value="' + e.target.fileName + '" >';
$("#data").empty().append(html);
}
debugger;
fr.readAsDataURL(file);
});
</script>
</body>
</html>
Note: This will require enabling advanced Drive service from within Google Apps Script - Menu > Resources > Advanced Google Services AND Menu > Resources > Advanced Google Services > Drive API and Admin Directory API