How to generate an uuid in google sheet?

前端 未结 6 1764
花落未央
花落未央 2021-02-04 02:24

How to generate an uuid in google sheet (for exemple ccb8a660-91c9-a556-58e0-4fa7-229516872004).

Either with a macro? or a formula?

6条回答
  •  轮回少年
    2021-02-04 02:57

    Thanks for all the details on this thread. What I ended up doing is creating a macro that will fill all the selected cells in a range each with their own UUID. I use this approach to set a value in selected cells and not worry about "overwriting" them. I can use Protect Range to avoid the cells being updated inadvertently.

    This script has 2 checks:

    1. selected range must cover just a single column
    2. all selected cells must be blank (i.e. no selected cell can have a value)

    If the above 2 constraints are met, then each cell gets filled with a value. Here's the macro code:

    function fillSelectedWithUUIDs() {
      let curSheet = SpreadsheetApp.getActiveSheet();
      let curSelection = curSheet.getSelection();
      let curRange = curSelection.getActiveRange();
    
      let ui = SpreadsheetApp.getUi();
    
      if (curRange.getNumColumns() !== 1) {
        ui.alert(`Range must only contain one column.`);
        return;
      }
    
      for (let i = 0; i < curRange.getNumRows(); i++) {
        let curCell = curRange.getCell(1 + i, 1);
        if (curCell.getValue() !== "") {
          ui.alert(`ERROR: Cannot overwrite value in cell (${curCell.getA1Notation()})`);
          return;
        }
      }
    
      for (let i = 0; i < curRange.getNumRows(); i++) {
        curRange.getCell(1 + i, 1).setValue(Utilities.getUuid())
      }
    
      ui.alert(`Added ${curRange.getNumRows()} UUIDs`);
    }
    

    Hope this is of use to some!

提交回复
热议问题