Create A Unique ID Using A Custom Function In Google Sheets
April 5, 2023 • Google Apps Script, Google Sheets • Published By Josh • 2 minute read
Have you ever needed to uniquely identify a row in Google Sheets? You could use the row number, but if users move or delete rows, the numbers would change. What you need is a unique id for each row. Learn how to create a custom function that returns a unique id in Google Sheets!
Table of Contents
Create A Custom Function
- Open your spreadsheet and navigate to Extensions > Apps Script.
- The Apps Script Editor will open with a single script file called Code.gs.
- An empty function will default in. You can remove it.
- Give your untitled project a name.
In the Apps Script code editor, create a function called UUID.
function UUID() {
}
Apps Script provides a Utilities class with a getUuid method. All we have to do is return it in the function.
return Utilities.getUuid();
Here’s the final code for the UUID function.
function UUID() {
return Utilities.getUuid();
}
Use A Custom Function
Using a custom function is no different than using the many functions Google Sheets provides. Within the cell, enter the function:
=UUID()
The result will be a unique id!
baceef3c-f633-4deb-94db-1c17a2105719
You can also use functions (and custom functions) when inserting rows. For example, when using the appendRow method of the Sheet class.
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Users');
sheet.appendRow([
'=UUID()',
'John',
'Smith'
]);
You can now uniquely identify rows using the id! This can be useful when making an edit to a specific row!