In many libraries, user data is collected through online forms such as Google Forms. While text data (name, course, admission number, etc.) is easy to process, handling uploaded photos becomes a challenge — especially when preparing for bulk upload in Koha.
In Koha, patron images must be named exactly as the cardnumber (e.g., 2024001.jpg) for bulk import. When collecting images via Google Forms, uploaded files are saved in Google Drive with system-generated filenames. Renaming them manually for hundreds or thousands of users is time-consuming and error-prone.
This blogpost explains how to automatically rename uploaded images using Google Apps Script, so that photos are saved directly with the admission number (or cardnumber) as the filename.
The Problem
- When using Google Forms with a file upload field:
- Images are saved in a default Google Drive folder.
- Filenames are random or original upload names.
- Koha requires filenames like: CARDNUMBER.jpg
For bulk patron image upload in Koha:
- Each image must match the exact cardnumber.
- Manual renaming is tedious for large datasets.
The Solution: Google Apps Script Automation
We can attach a Google Apps Script to the Google Form (or linked Sheet) to:
- Detect new form submissions
- Fetch the uploaded image
- Rename it using the Admission Number / Cardnumber
- Move it to a specific folder
- Delete the original file
Implementation Steps
Step 1: Create a Folder in Google Drive
Create a folder in Google Drive where renamed images will be stored.
Copy the Folder ID from the URL:
https://drive.google.com/drive/folders/FOLDER_ID_HERE
Step 2: Open Apps Script
- Open your Google Form
- Click Responses → View in Sheets
- In the Sheet: Extensions → Apps Script
- Replace the default code with the script below
Step 3: Use This Script
function onFormSubmit(e) {
try {
// Replace with your folder ID
const folderId = "1LPxTWKhUyTfIjjj2KA-VmONbhfOlO4SI"; // Folder where renamed images will be stored
const folder = DriveApp.getFolderById(folderId);
const responses = e.values;
// Adjust index according to your form columns:
// 0 = Timestamp
// 1 = Name
// 2 = Course
// 3 = Admission Number (Cardnumber)
// 4 = WhatsApp
// 5 = DOB
// 6 = File Upload
const admissionNumber = responses[3];
const fileUrl = responses[6];
// Extract file ID from URL
const fileId = fileUrl.match(/[-\w]{25,}/);
if (!fileId) throw new Error("File ID not found in URL");
const file = DriveApp.getFileById(fileId[0]);
// Make a renamed copy in target folder
const newFile = file.makeCopy(`${admissionNumber}.jpg`, folder);
// Optional: Remove original file
file.setTrashed(true);
} catch (error) {
Logger.log("Error: " + error);
}
}
Step 4: Set the Trigger
- In Apps Script → Click Triggers (Clock icon)
- Click + Add Trigger
- Choose: Function: onFormSubmit , Event Source: From spreadsheet, Event Type: On form submit
- Save and authorize permissions.
Each time a user submits the form, the uploaded image is automatically renamed and saved using the format CARDNUMBER.jpg. The file is then stored in your selected Google Drive folder, making it instantly organized and ready for Koha bulk upload process.
