//This section just for the example - to allow images to be replaced from trash var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[1]; sheet.appendRow([e.parameter.id]); //end of example section
Here is the code for the web app, which is a script bound to the spreadsheet
function doGet() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var rngVal = sheet.getRange("A1").getValue(); var newVal = rngVal + 1; sheet.getRange("A1").setValue(newVal); return ContentService.createTextOutput(newVal);}
//for PC browserfunction doPost() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var rngVal = sheet.getRange("A1").getValue(); var newVal = rngVal + 1; sheet.getRange("A1").setValue(newVal); return ContentService.createTextOutput(newVal);}
When you publish the script as a web app, you should run it as you, but ensure that anyone can access it. Every time you make a change to the web app you must publish a new version.
You can also run the web app in your browser using the web app url, it will return a white page with the latest number in the top left corner:
https://script.google.com/macros/s/AKfycbz9XZ1YU_yaSsiJNqbk41p58b5ZgQpbrQ_aBfGPlmG5x8UId6m3/exec
Here is the link for the spreadsheet, so you can see the sheet updating as you activate the web app:
https://docs.google.com/spreadsheets/d/1YbuuoptAWtj4O9eIzipUzSRj5uvauJAqx5SSxjWq60w/edit#gid=0
For your purposes, you should remove the code section from your script, then you do not need the second sheet, unless you want a record of the files deleted on the second sheet
function doGet(e) {return message("Error: no parameters in doGet"); }
function doPost(e) { if (!e.parameters.filename || !e.parameters.file || !e.parameters.imageformat) { return message("Error: Bad parameters in doPost"); } else { var imgf = e.parameters.imageformat[0].toUpperCase(); var mime = (imgf == 'BMP') ? MimeType.BMP : (imgf == 'GIF') ? MimeType.GIF : (imgf == 'JPEG') ? MimeType.JPEG : (imgf == 'JPG') ? MimeType.JPEG : (imgf == 'PNG') ? MimeType.PNG : (imgf == 'SVG') ? MimeType.SVG : false; if (mime) { var data = Utilities.base64Decode(e.parameters.file, Utilities.Charset.UTF_8); var blob = Utilities.newBlob(data, mime, e.parameters.filename); DriveApp.getFolderById('FOLDER ID HERE').createFile(blob);
getFileIdAndNames();
return message("Success"); } else { return message("Error: Bad image format"); } }}
function message(msg) { return ContentService.createTextOutput(JSON.stringify({Result: msg })).setMimeType(ContentService.MimeType.JSON);}
function getFileIdAndNames() {
var FileFolderID = 'ENTER YOUR IMAGES FOLDER ID HERE'; // the ID of the folder with files var ss = SpreadsheetApp.openById('ENTER YOUR SPREADSHEET ID HERE');
var sheet = ss.getSheets()[0];
var urlCol = 1; var urlRow = 1; var folder = DriveApp.getFolderById(FileFolderID); var files = folder.getFiles();
sheet.clear();
while (files.hasNext()) { var file = files.next(); if (file.getName().indexOf(".jpg") != -1) { sheet.getRange(urlRow, urlCol).setValue(file.getName()); sheet.getRange(urlRow, urlCol+1).setValue(file.getId()); sheet.getRange(urlRow, urlCol+2).setValue('https://drive.google.com/uc?export=view&id='+ file.getId()); sheet.getRange(urlRow, urlCol+3).setValue('JPG'); sheet.getRange(urlRow, urlCol+4).setValue(Math.round(file.getSize()/1000) + " KB"); sheet.getRange(urlRow, urlCol+5).setValue(file.getName().slice(0, -4)); } else if (file.getName().indexOf(".png") != -1) { sheet.getRange(urlRow, urlCol).setValue(file.getName()); sheet.getRange(urlRow, urlCol+1).setValue(file.getId()); sheet.getRange(urlRow, urlCol+2).setValue('https://drive.google.com/uc?export=view&id='+ file.getId()); sheet.getRange(urlRow, urlCol+3).setValue('PNG'); sheet.getRange(urlRow, urlCol+4).setValue(Math.round(file.getSize()/1000) + " KB"); sheet.getRange(urlRow, urlCol+5).setValue(file.getName().slice(0, -4)); } urlRow = urlRow+1; } sheet.sort(1, true); sheet.sort(4, true); sheet.insertRows(1); var title = [["Filename","File ID","Direct Link to View","File Type","File Size (KB)","Description"]]; var fontStyles = [ [ "bold", "bold", "bold", "bold", "bold","bold" ]]; var titleRange = sheet.getRange("A1:F1"); titleRange.setValues(title); titleRange.setFontWeights(fontStyles); }