0

I'd created a Google form for collecting data from users. In this form I'm going to ask the site name and want to replace it with Unique code.

https://docs.google.com/forms/d/1JTStfAZGFBAFjVxgH0aZlMUroVsBijC0sfOvuXvqce8/viewform?usp=send_form#start=invite

I'd written the codes with google apps script for this which is given below but every time it generate new unique code and also replace previous one also.

function SendConfirmationMail(e) {
  try {
    var ss, bcc, sendername, subject, columns, username;
    var message, value, textbody, usermail, track, code;

    // This is your email address and you will be in the BCC
    bcc = Session.getActiveUser().getEmail();

    // This will show up as the sender's name
    sendername = "Support Team";

    // This is the submitter's Name
    username = e.values[1];

    // This is the submitter's email address
    usermail = e.values[2];

    // Custom subject for Google Docs emails
    subject = "Support Ticket";

    // Random Code for Google Docs emails
    ss = SpreadsheetApp.getActiveSheet();
    track = new Array();
    var chars = "ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
    var string_length = 10;
    var lastRow = ss.getLastRow()-1;
    var randomstring = '';
    for (var i=0; i<string_length; i++) {
      var rnum = Math.floor(Math.random() * chars.length);
      randomstring += chars.substring(rnum, rnum+1);
    }
    track.push(randomstring);

    code = ss.getRange(2, 5, lastRow, 1).setValue(track);

    // This is the body of the auto-reply
    message = "Hi <b>" +username+ " !!!</b> <br><br> Your message has been successfully received.<br>";


    GmailApp.sendEmail(usermail, subject, messag, 
                       {bcc: bcc, name: sendername, htmlBody: message});

    } catch (e) {
        Logger.log(e.toString());
    }

}

But I want to generate unique code for every submission. Please update me.

AstroCB
  • 12,337
  • 20
  • 57
  • 73
dizaraj
  • 13
  • 5

1 Answers1

1

I've struggled with this sort of thing before, and the complexity depends on the security it needs (as far as I can tell, this is all internal, so not much).

I'm not sure I understand why you'd put your unique code as an editable text box (I'm assuming site name is just a holdover and you would eventually either make it a non-editable div or not show it at all).

Either way, one method is to keep an array of all your previous unique codes in properties, although if this is used a lot, then that could be too much (look at the quotas, which Wolfram Alpha says is about 500 words conservatively).

So, maybe you could have a few scriptDBs (or even a spreadsheet, although it's slower) that you combine to get a final array of all your previous codes. Your randomstring algorithm could be fine (as long as you check to make sure it doesn't exist in the overarching scriptDB array).

Look at Generate random string/characters in JavaScript for other random string ideas, but just make sure (again, if this gets very popular) that there are enough new possibilities.

You don't want this hanging while looking for a new code, so I would also add a counter and do something else if it takes more than 100 iterations (if this is all internal, it might not be a bad idea to have a 5-10 character code just to keep the chances of it not finding a new string quickly much lower).

Community
  • 1
  • 1
JZL003
  • 426
  • 5
  • 16