Generate Reference Number to Google Sheets automatically using Google Apps Script - javascript

I am trying to generate a reference number every time a new data in inserted automatically from Google Form.
I would like to generate the format as TMS180001 until the maximum possible reference number which is TMS189999. I could not figure how to format the value/string to 0001 instead of 1 and when I ran below code nothing happened.
function onFormSubmit(e) {
var Time = e.values [0]; //column 1 timestamp
var TMSrefnum = [10]; //column 1 till 9 are other information while
//column 10 is the reference number
if (Time = true); //if timestamp has value then add reference
//number to column 11
var i = 1 ; i < 9999 ; i++;
TMSrefnum = "TMS18" + toString(i);
}

Zero Padding
For zero padding use your own functions e.g.
Number.prototype.pad = function(size) {
var s = String(this);
while (s.length < (size || 2)) {s = "0" + s;}
return s;
}
(9).pad(); //returns "09"
(7).pad(3); //returns "007"
This will allow you to execute the .pad() method on any Number in your script.
Or write a method that looks something like this:
function pad(num, size) {
var s = "000000000" + num;
return s.substr(s.length-size);
}
pad(98); // returns "000000098"
Sources:
https://stackoverflow.com/a/11187738/1139105
https://stackoverflow.com/a/2998822/1139105

See this answer to the question How to output integers with leading zeros in JavaScript.
You'll need to create an extra function outside the onFormSubmit() function (or include the Polyfill).
function pad(num, size) {
var s = num+"";
while (s.length < size) s = "0" + s;
return s;
}
Then you would call
TMSrefnum = "TMS18" + pad(currentEntryNumber, 4);

I managed to produce the reference number from below code.
var sheet1 = ss.getSheets()[0];
function pad(num, size) {
var s = num + "";
while (s.length < size) s = "0" + s;
return s;
}
var valuerange = sheet1.getRange("A1:A").getValues();
var lastrownumber = valuerange.filter(String).length-1;
var TMSNUM = "TMS18" + pad(lastrownumber, 4);

Related

Converting a 64 bit HEX to Decimal Floating-Pointt in Javascript

So basically I have a couple of numbers that come out as HEX values in the form of "3FF0000000000000" and I want to get float values of out these, pretty much like in here So in this particular case I'd expect "20.000000000000000" as a result - which I'll later trim to only 5 decimals, but that should be easy enough.
I've tried a couple of solutions but unfortunately I know too little about conversions (and javascript aswell) to know exactly what I might be doing wrong.
The latest try looks something like this:
const hexToFloat64 = (hex) => {
var int32 = [],
float64;
if (hex.length > 4) {
var high = hex.substr(0, 8),
low = hex.substr(8, 8);
int32.push(parseInt(high, 16), parseInt(low, 16));
}
var uint32 = new Uint32Array(int32);
float64 = new Float64Array(uint32.buffer);
var returnValue = float64[0];
return returnValue;
};
Much obliged!
This is NOT an answer to your exact problem. It IS a solution to decode the hex. Thats all i am doing here. I have no context to solve your problem.
function convHexStringToString(ss) {
// ss length must be even (or 0) when passed to this function
var s = "";
var p;
if (ss.length > 0) {
if (ss.length % 2 == 0) {
var l = Math.floor(ss.length / 2); // floor must never have to do work
for (var i = 0; i < l; i++) {
var i2 = i * 2;
if (ss.charAt(i2) != "0") {
p = ss.charAt(i2) + ss.charAt((i2) + 1);
}
else {
p = ss.charAt((i2) + 1);
}
d = parseInt(p,16);
s += String.fromCharCode(d);
}
}
}
return s;
}

Sum cells if they are not bold

I'm confused with my Google Apps script which is purposed to calculate the sum of the cells only if these cells are bold.
Here is the source:
function SumIfNotBold(range, startcol, startrow){
// convert from int to ALPHANUMERIC
// - thanks to Daniel at http://stackoverflow.com/a/3145054/2828136
var start_col_id = String.fromCharCode(64 + startcol);
var end_col_id = String.fromCharCode(64 + startcol + range[0].length -1);
var endrow = startrow + range.length - 1
// build the range string, then get the font weights
var range_string = start_col_id + startrow + ":" + end_col_id + endrow
var ss = SpreadsheetApp.getActiveSpreadsheet();
var getWeights = ss.getRange(range_string).getFontWeights();
var x = 0;
var value;
for(var i = 0; i < range.length; i++) {
for(var j = 0; j < range[0].length; j++) {
if(getWeights[i][j].toString() != "bold") {
value = range[i][j];
if (!isNaN(value)){
x += value;
}
}
}
}
return x;
Here is the formula:
=(SumIfNotBold(K2:K100,COLUMN(K2), ROW(K2)))*1
I have three major concerns:
When I set up a trigger to launch this script on any edits I accidentally receive an email from Google Apps stating that
TypeError: Cannot read property "length" from undefined. (line 7, file
"SumIfNotBold")
Thus, how can I fix it? Are there any ways to ignore these automatically delivered notifications?
The formula doesn't calculate the sum of cells if they are on the other list. For example, if I put the formula on B list but the cells are located on A list then this script doesn't work properly in terms of deriving wrong calculations.
When the cell values are updated the formula derivation is not. In this case I'm refreshing the formula itself (i.e., changing "K2:K50" to "K3:K50" and once back) to get an updated derivation.
Please, help me with fixing the issues with this script. Or, if it would be better to use a new one to calculate the sum in non-bold cells then I'll be happy to accept your new solution.
Here is a version of this script that addresses some of the issues you raised. It is invoked simply as =sumifnotbold(A3:C8) or =sumifnotbold(Sheet2!A3:C8) if using another sheet.
As any custom function, it is automatically recalculated if an entry in the range to which it refers is edited.
It is not automatically recalculated if you change the font from bold to normal or back. In this case you can quickly refresh the function by delete-undo on any nonempty cell in the range which it sums. (That is, delete some number, and then undo the deletion.)
Most of the function gets a reference to the passed range by parsing the formula in the active cell. Caveat: this is based on the assumption that the function is used on its own, =sumifnotbold(B2:C4). It will not work within another function like =max(A1, sumifnotbold(B2:C4).
function sumifnotbold(reference) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var formula = SpreadsheetApp.getActiveRange().getFormula();
var args = formula.match(/=\w+\((.*)\)/i)[1].split('!');
try {
if (args.length == 1) {
var range = sheet.getRange(args[0]);
}
else {
sheet = ss.getSheetByName(args[0].replace(/'/g, ''));
range = sheet.getRange(args[1]);
}
}
catch(e) {
throw new Error(args.join('!') + ' is not a valid range');
}
// everything above is range extraction from the formula
// actual computation begins now
var weights = range.getFontWeights();
var numbers = range.getValues();
var x = 0;
for (var i = 0; i < numbers.length; i++) {
for (var j = 0; j < numbers[0].length; j++) {
if (weights[i][j] != "bold" && typeof numbers[i][j] == 'number') {
x += numbers[i][j];
}
}
}
return x;
}

limit the query exactly with 8 and 7 digits in javascript

i have a little question:
how i can limit or generate the result of a query in 7 or 8 digits
example:
var x = 3143284294
var y = 387520525892
var z = -7632489234892
var w = 34563
result:
var x = 3143284
var y = 3875205
var z = -763248
var w = 3456300 (fill whit "0")
What function or prefix in javascript will use?
tnks(and sorry for my english)
This converts the number to a string and performs string operations on it. Note that repeat is a fairly recent feature of ECMAScript.
function cutNum(n, limit) {
n = n + '';
n = n.substr(0, limit);
if (n.length < limit) {
n = n + '0'.repeat(limit - n.length);
}
return parseInt(n, 10);
}
var x = 3143284294;
cutNum(x, 7); // 3143284
var z = -7632489234892;
cutNum(z, 7); // -763248
var w = 34563;
cutNum(w, 7); // 3456300
Take a look at the slice() method.
var numbers = "01234567890";
var res = numbers.slice(0,6);
alert(res);
Since your sample also includes that are less than 7 digits, you will want to run a logic check first prior to the slice.
var x = "01"
if(x.length < 7)
x = x + "0000000";
x = x.slice(0,6);
alert(x);

Javascript: split time

I am trying to format a variable value that contains time data and is like the following 00:38:51 or 00:00:59 or 01:25:59
I need to format it like 25m59s or with hour accordingly 1h25m59s 0m59s
I am doing some reading on splits and I have made a start but I am getting confused.
Syntax
Split(expression[,delimiter[,count[,compare]]])
time = "00:38:51"
timeArray = time.split(":",-1)
document.write(timeArray[0]);
Update
I created my own function, I am not sure if this is a good way to do it also;
function formatTime(a) {
var time = a.split(":");
var hours = parseInt(time[0], 10);
var minutes = parseInt(time[1], 10);
var seconds = parseInt(time[2], 10);
var x = document.getElementById("time");
x.innerHTML = hours + "h" + minutes + "m" + seconds + "s"
}
myTime = "00:38:51";
formatTime(myTime);
http://jsfiddle.net/V64dJ/
Try with RegExp
var arr = ["00:38:51","00:00:59","01:25:59"]; // created array due to demonstration
var reg = /(\d+):(\d+):(\d)/, ret = []; // RegExp and result variable declaration
arr.forEach(function(v){
ret = reg.exec(v);
console.log(parseInt(ret[1]) + "h" + parseInt(ret[2]) + "m" + parseInt(ret[3]) + "s"); // 0h38m5s, 0h0m5s, 1h25m5s
});
JSFiddle
NOTE: Using parseInt function during displaying results due to avoid printing 00 instead of 0
This should do it!
function a(time) {
var t = time.split(':'),
s = ['h', 'm', 's'],
i = 0;
for(; i < t.length; i ++) {
t[i] = parseInt(t[i]) == 0 ? '' : parseInt(t[i]) + s[i + s.length - t.length];
}
return t.join('');
}
Fiddle

How do I remove "undefined" from the beginning of JavaScript array items?

I'm trying to generate an array of random digits, but I'm getting "undefined" at the beginning of each row. I've been searching online for a couple of hours, but haven't been able to figure it out.
The expected output should be 5 rows of 2 random digits like this:
87
57
81
80
02
but the actual output looks like this:
undefined87
undefined57
undefined81
undefined80
undefined02
This is a modified excerpt that produces the result shown above:
function NumberSet() {
// generate all the digits
this.generate = function() {
random_digits = [];
// create 5 rows of 2 random digits
for(i=0; i<5; i++) {
for(z=0; z<2; z++) {
// use .toString() in order to concatenate digits to
// the array without adding them together
random_digit = Math.floor(Math.random()*10).toString();
random_digits[i] +=random_digit;
}
}
return random_digits;
}
}
randomnumbers1 = new NumberSet();
mynums = randomnumbers1.generate();
jQuery.each(mynums, function(i, l) {
// display output in a div#nums
$('#nums').append(l + '<br>');
});
The final version won't be using this method to display the digits. I'm just trying to troubleshoot where the "undefined" is coming from.
Initialize your variables
random_digits[i] = "";
for(z=0; z<2; z++) {
random_digit = Math.floor(Math.random()*10).toString();
random_digits[i] +=random_digit;
}
Declare the variables properly with var.
var random_digit, random_digits = [];
Declare random_digit in the first for loop and assign an empty string.
Go through the inner for loop appending your random numbers, and then push() to the array back in the outer for loop.
function NumberSet() {
// generate all the digits -a meme should be attached here-
this.generate = function() {
random_digits = [];
// create 5 rows of 2 random digits
for(i=0; i<5; i++) {
var random_digit = ""; //Declare it out here
for(z=0; z<2; z++) {
// use .toString() in order to concatenate digits to
// the array without adding them together
random_digit += Math.floor(Math.random()*10).toString(); //Append it here
}
random_digits.push(random_digit); //Push it back here
}
return random_digits;
}
}
Fiddle-dee-dee
OR Forget the inner loop and use recursion
function NumberSet() {
// generate all the digits
this.generate = function () {
random_digits = [];
// create 5 rows of 2 random digits
// Use i for how many numbers you want returned!
var random_digit = function (i) {
var getRand = function() {
return (Math.floor(Math.random() * 10).toString());
}
return (i > 0) ? getRand()+random_digit(i-1) : "";
};
for (i = 0; i < 5; i++) {
random_digits.push(random_digit(2)); //In this case, you want 2 numbers
}
return random_digits;
}
}
Fiddle-do-do
And the final version because I'm bored
function NumberSet(elNum, numLen) {
this.random_digits = []; //Random digits array
this.elNum = elNum; //Number of elements to add to the array
this.numLen = numLen; //Length of each element in the array
// generate all the digits
this.generate = function () {
// create 5 rows of 2 random digits
var random_digit = function (i) {
var getRand = function () {
return (Math.floor(Math.random() * 10).toString());
}
return (i > 0) ? getRand() + random_digit(i - 1) : "";
};
for (i = 0; i < this.elNum; i++) {
this.random_digits.push(random_digit(this.numLen));
}
return this.random_digits;
}
}
randomnumbers1 = new NumberSet(5, 2).generate();
jQuery.each(randomnumbers1, function (i, l) {
// display output in a div#nums
$('#nums').append(l + '<br>');
});
Fiddle on the roof
Replace
random_digits[i] +=random_digit;
With
random_digits[i] = (random_digits[i] == undefined ? '' : random_digits[i]) + random_digit;
Demo: Fiddle
Your function can be simplified to:
function NumberSet() {
this.generate = function() {
var random_digits = new Array();
for (i = 0; i < 5; i++) {
randnum = Math.floor(Math.random() * 99);
random_digits[i] = (randnum < 10 ? '0' : 0) + randnum;
}
return random_digits;
}
}
Live Demo

Categories