Monday 9 September 2013

Upload text file to Google Drive with Google Apps Script

Upload text file to Google Drive with Google Apps Script

Seems like this should be easier. But admittedly I don't understand blobs.
function doGet(e) {
var app = UiApp.createApplication();
var panel = app.createVerticalPanel().setId('panel');
var fileUpload =
app.createFileUpload().setName('theFile').setId('theFile');
var handler = app.createServerChangeHandler('uploadfile');
handler.addCallbackElement(panel);
fileUpload.addChangeHandler(handler);
panel.add(fileUpload);
app.add(panel);
return app;
}
function uploadfile(e)
{
// data returned which can be used to create a blob
// assuming mime-type to be a text file in this example
var fileBlob = Utilities.newBlob(e.parameter.thefile,
"text/plain","file.txt" );
// Create a new file
var doc = DocumentApp.create('Uploaded Text File');
doc.appendParagraph(fileBlob.getDataAsString());
// Save and close the document
doc.saveAndClose();
//var doc = DocsList.createFile(fileBlob.getDataAsString());
var app = UiApp.getActiveApplication();
app.getElementById('panel').add(app.createLabel('File Uploaded
successfully'));
return app;
}
I keep getting undefined returned when I attempt to upload a file using
this Google Apps Script Code. All I want to do is upload a text file to my
Google Drive.
What should I do to fix this code? or is there a better way to do this?

No comments:

Post a Comment