Quando ficheiros do google sheets são utilizados para fornecer dados à plataforma a criação de um UUIDv4 permite manter um link entre uma linha da spreadsheet e uma entrada na plataforma. Assim quando uma linha é alterada ou removida a plataforma consegue encontrar a entrada que deve alterar.
O código que se segue é um script do google app scripts que adiciona um UUID na primeira coluna desde que algum valor dessa linha esteja preenchido
function onEdit(e) {
// Get the active sheet
var sheet = e.source.getActiveSheet();
var editedRange = e.range; // Get the range that was edited
var row = editedRange.getRow(); // Get the row number
var colCount = sheet.getLastColumn(); // Get the number of columns in the sheet
// Get the entire row of data
var rowData = sheet.getRange(row, 1, 1, colCount).getValues()[0];
// Check if any cell in the row has content (except the first column)
var hasContent = rowData.slice(1).some(cell => cell !== "" && cell !== null);
// If there is content in any column of the row (excluding the first column) and the first column is empty
if (hasContent && !rowData[0]) {
// Generate a UUID and place it in the first column
var uuid = Utilities.getUuid();
sheet.getRange(row, 1).setValue(uuid);
}
// If all cells in the row are empty except the first one, clear the first column
if (!rowData.slice(1).some(cell => cell !== "" && cell !== null) && rowData[0]) {
sheet.getRange(row, 1).clearContent(); // Clear the content of the first column
}
}
