Karan Patel
Karan Patel

Reputation: 89

Google Apps Script random string generating

i am new to Google apps script, i want to create string of random characters in the code given below in variable body2.

function myfunction() {
var files = DriveApp.getFiles();
while (files.hasNext(`enter code here`)) {
Logger.log(files.next().getName());
}
var recipient = Session.getActiveUser().getEmail();
var subject = 'A list of files in your Google Drive';
var body1 = Logger.getLog(); 
var body2;
for(var i=0;i<6;i++)
{   
body2[i]=BigNumber.tostring("Math.floor(Math.random()*11)");
}

  body=body1+body2;
 MailApp.sendEmail(recipient, subject, body);
};

but when i run this function, it says "TypeError: Cannot find function tostring in object 0. (line 12, file "Code") " i can't understand how to solve this error? Why we have to multiply random by 11 , can it be multiplied with any integer number? what if i want that string in only capital letters.!

Some other question 1) i don't have enough knowledge of JavaScript, is it good to learn GAS directly? 2) i can't find proper written material or documentation for GAS , the material available at Google's official site is seems to be updating time by time , what to do then ? any link to material would help me .!

Upvotes: 2

Views: 11484

Answers (3)

Syed Hussaini
Syed Hussaini

Reputation: 131

I guess I just figured

function randomStr(m) {
    var m = m || 15; s = '', r = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    for (var i=0; i < m; i++) { s += r.charAt(Math.floor(Math.random()*r.length)); }
    return s;
};

Hope someone finds it helpful.

Upvotes: 11

Jacobvdb
Jacobvdb

Reputation: 1448

I have this charIdGeneration() in my GAS library

function charIdGenerator()
 {
     var charId  ="";
       for (var i = 1; i < 10 ; i++) 
       { 
           charId += String.fromCharCode(97 + Math.random()*10);
       } 
     //Logger.log(charId)
     return charId;    
 } 

Upvotes: 1

Zig Mandel
Zig Mandel

Reputation: 19835

As for a random string use this its better: Math.random().toString(36). 36 is the base thus will use letters and numbers in the string. As for gas documentation, the official page is pretty complete. It changes because it constantly improves and adds new services.

Upvotes: 1

Related Questions