I had the same issue today and came across this question.
I adapted it to iterate through all the rows automatically, along with some variables to make it easier to customize (choosing Name Column and URL Column).
It has functions for either renaming "all" the records (useful when you are renaming an existing form's records what I did :) or just the "last" one. The last one is nice to pair with an OnFormSubmitted Trigger (Targeting the "last" function) to avoid going through all the records every time, you can read more about it here.
Thank you for sharing your code, it really helped me out!
Here's the code if anyone needs it:
var sheet=ss.getActiveSheet();
var rangeData = sheet.getDataRange();
var lastColumn = rangeData.getLastColumn();
var lastRow = rangeData.getLastRow();
// Notice the second 2, this is to avoid the Timestamp Column
var searchRange = sheet.getRange(2,2, lastRow-1, lastColumn-1);
// Replace with your values (Column A=1, B=2, etc...)
var nameColumn = 2; // B
var urlColumn = 5; // E
// Calculating index for array
nameColumn -= 2;
urlColumn -= 2;
// Use this to rename the last record
function last() {
var lastRowContents=sheet.getRange(lastRow,2,1,sheet.getLastColumn()).getValues()[0];
rename(lastRowContents);
}
// Use this to rename all records
function all() {
// Put rows in an array
var rangeValues = searchRange.getValues();
// Loop through the rows and rename file
for ( i = 0 ; i < lastRow - 1; i++){
row = rangeValues[i];
rename(row);
};
}
// Retrieves the ID and Name fields from the row, then
// renames the file
function rename(row) {
// Using the first field, Name (Index 0 becuse of the array, calculated above)
// ** Even though the Name field is the second column, we see it as the first one since
// we ignored the timestamp column in the searchRange **
var userName = row[nameColumn];
var url = row[urlColumn];
// Retrieve the ID from the URL
var Id = url.split('=')[1];
// Adapt this newFileName to your needs
var newFileName = userName;
// Get the file and rename it
DriveApp.getFileById(Id).setName(newFileName);
Logger.log("Renamed file with ID " + Id + " to " + newFileName);
};