How to attach a file to google spreadsheet
Update 2022: There is a new solution which actually works! FileDrop is a google sheet extension which let’s you…
- choose a file from your computer to upload
- inserts the link to the file into the current cell
In order to use it:
- go to https://getfiledrop.com and click “install addon”
- on the spreadsheet you want to use it click extensions -> FileDrop -> Start FileDrop
- put the focus on the cell you want to add the link
- drag and drop the file into the sidebar
The file will be uploaded into your google drive into a folder “FileDrop” which you can move to any position of your drive tree. If you want to define the upload location then you need to switch to the paid version.
Alternatively you can use dropspread - but I found dropspread harder to use (it has a few UI quirks)
The old solution, for history reasons. I couldn’t get it to work again, if anyone is interested, I think this stackoverflow answer is pointing into the right direction.
The following setup lets you
To get it working:
- find out the id of the google drive folder you want your attachments be saved (in the example below this is
0B0uw1JCogWHuc29FWFJMWmc3Z1k
) - in the spreadsheet where you want to upload the file: do Tools→Script Editor.. and paste the script below. Be sure to replace the id with your folders id
Update 2014: Made it working with the new sheets
Update 2016-05 Fixed the script thanks to the comments by Fede and Dov (DocsList.getFolderId was not working any more)
// upload document into google spreadsheet
// and put link to it into current cell
function onOpen(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var menuEntries = [];
menuEntries.push({name: "File...", functionName: "doGet"});
ss.addMenu("Attach ...", menuEntries);
}
function doGet(e) {
var app = UiApp.createApplication().setTitle("upload attachment into Google Drive");
SpreadsheetApp.getActiveSpreadsheet().show(app);
var form = app.createFormPanel().setId('frm').setEncoding('multipart/form-data');
var formContent = app.createVerticalPanel();
form.add(formContent);
formContent.add(app.createFileUpload().setName('thefile'));
// these parameters need to be passed by form
// in doPost() these cannot be found out anymore
formContent.add(app.createHidden("activeCell", SpreadsheetApp.getActiveRange().getA1Notation()));
formContent.add(app.createHidden("activeSheet", SpreadsheetApp.getActiveSheet().getName()));
formContent.add(app.createHidden("activeSpreadsheet", SpreadsheetApp.getActiveSpreadsheet().getId()));
formContent.add(app.createSubmitButton('Submit'));
app.add(form);
SpreadsheetApp.getActiveSpreadsheet().show(app);
return app;
}
function doPost(e) {
var app = UiApp.getActiveApplication();
app.createLabel('saving...');
var fileBlob = e.parameter.thefile;
var doc = DriveApp.getFolderById('0B0uw1JCogWHuc29FWFJMWmc3Z1k').createFile(fileBlob);
var label = app.createLabel('file uploaded successfully');
// write value into current cell
var value = 'hyperlink("' + doc.getUrl() + '";"' + doc.getName() + '")'
var activeSpreadsheet = e.parameter.activeSpreadsheet;
var activeSheet = e.parameter.activeSheet;
var activeCell = e.parameter.activeCell;
var label = app.createLabel('file uploaded successfully');
app.add(label);
SpreadsheetApp.openById(activeSpreadsheet).getSheetByName(activeSheet).getRange(activeCell).setFormula(value);
app.close();
return app;
}
Comments