Home > Blockchain >  Keeping a system generated UUID from changing when the script is run again
Keeping a system generated UUID from changing when the script is run again

Time:06-14

I have this script that generates UUID's for a project. When the script runs I do not want to replace the UUID's that already exist. What do I need to add to this script to keep the existing UUID's from regenerating? An if statement that will not setvalues if the cell has data in it? Right now this code will overwrite the existing UUID's with new ones. I am using the UUID in a vlookup, so I can't have the existing ones changing. thank you.

function generateInviteIds() {

  const ss = SpreadsheetApp.getActiveSpreadsheet()
  const ws = ss.getActiveSheet()
  const lr = ws.getRange(ws.getMaxRows(), 6).getNextDataCell(SpreadsheetApp.Direction.UP).getRow()

  const idRange = ws.getRange(1, 3, lr, 1).getValues()

  //console.log(nr)
  //console.log(lr)
  //console.log(idRange)

  const inviteIds = []

  for (let i = 1; i < lr; i  ) {
    const uid = Utilities.getUuid()
    inviteIds.push([uid])

  }
  

   ws.getRange(2, 3, inviteIds.length, 1).setValues(inviteIds)
  
 


}

CodePudding user response:

Use Array.map(), like this:

function generateInviteIds() {
  const sheet = SpreadsheetApp.getActiveSheet();
  const idRange = sheet.getRange('C1:C'   sheet.getLastRow());
  const ids = idRange.getValues()
    .map(row => row.map(id => id || Utilities.getUuid()));
  idRange.setValues(ids);
}
  • Related