Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

How do you create a Uniq ID in Google Sheets using apps script?

Im looking at creating a simple Uniq ID after column A has information entered and need for the ID to show on column I. I want to call it Trip Number and display Driver-John0001 and so forth using google sheets script. Sorry I'm new to this so I don't know the lingo

The current code I had found works but now I need it a bit different. This is what my results show Driver:1611710811706

I would like to pull Driver-John0001. Where the name John is generated by the column labeled Driver or column D How do I change it to add the value on column D + 4 digit numbers that don't repeat?

enter image description here

function TripID () {
     { var sheetVals = [
      ["DriverLog","Driver:","",9,1]
    ];}
 
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sheet = ss.getActiveSheet();
  
      //Loop through every sheet value
      for(var i = 0; i < sheetVals.length; i++){
        var sheetID  = sheetVals[i][0],
            frontStr = sheetVals[i][1],
            backStr  = sheetVals[i][2],
            IDcol    = sheetVals[i][3],
            editCol  = sheetVals[i][4];
    
        var offset = IDcol - editCol;
    
        if(sheet.getName() === sheetID){
          var date = new Date().getTime();
          var newID = frontStr+date+backStr;   
      
          //Check the location of the active cell
          var selectedCell = ss.getActiveCell();
          if( selectedCell.getColumn() === editCol) {
      
            //Update the ID Column
            var cellToChange = selectedCell.offset(0,offset);
            if(cellToChange.isBlank()){
            cellToChange.setValue(newID);
            };
          };
        };
      };
    }; 
question from:https://stackoverflow.com/questions/65912486/how-do-you-create-a-uniq-id-in-google-sheets-using-apps-script

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Assuming that you will never need more than 9999 IDs, then you could modify your code with the following steps to accomplish this. Note that this solution will allow you to create a (nearly) unlimited number of ID numbers, but after 9999, the length of the id numbers will vary.

  1. Use the PropertiesService to initialize a counter for creating IDs
  function createIdProperty() {
     PropertiesService.getScriptProperties().setProperty('idCounter', '0');
  }
  
  // run this once to create the idCounter property
  createIdProperty();

  // To test that your property was created you can run this
  Logger.log(PropertiesService.getScriptProperties().getProperty('idCounter')); // logs '0'
  1. Create a function to get the next counterId number from Script Properties as a string with 0s replacing empty digits ( 1 => '0001', 2 => '0002', 3 => '0003')
function getUniqueIdNumber() {
  var currentIdNum = +PropertiesService.getScriptProperties().getProperty('idCounter'); //get counter as a number
  currentIdNum++; // increment counter by 1
  currentIdNum+=''; // convert counter back to a string
  PropertiesService.getScriptProperties().setProperty('idCounter', currentIdNum); // store the new counter in properies
  while (currentIdNum.length < 4) {
    currentIdNum = '0'+currentIdNum;
  }
  return currentIdNum;
} 
  1. modify your original code to use getUniqueIdNumber instead of date for creating your id

Change

var newID = frontStr+date+backStr;

To

var idNumber = getUniqueIdNumber();
var newID = frontStr+idNumber;

Note that you can replace frontStr and backStr with any variables you want.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...