I wrote a small script in order to have an incremental ID composed by string character by using the function Utilities.formatString
Example: RQ001 -> RQ002 --> RQ00n
function myFunction() {
var str = "RQ001";
var res = str.substring(2, 5); 'ok
res=Number(res)+1; 'ok
res=res.toString(); 'ok
res = "RQ" & Utilities.formatString("000", res); 'not working }
The results is "0".
Thank you in advance for your help.
& is not string concatenation operator. Use "+" instead.
You have two issues. You need to use "+" for string concatenation and your method for creating your string isn't properly returning "002" like you want.
function myFunction() {
var str = "RQ001";
var res = Number(str.substring(2, 5)) + 1;
res = "000" + String(res);
res = "RQ" + res.substring(res.length - 3, res.length);
}
Use substring() to format your string's number and then append the "RQ" to the beginning afterwards.
You can also format the function to take the original key as a parameter. This way, you can say something like newKey = incrementKey(oldKey).
function incrementKey(str) {
var res = Number(str.substring(2, 5)) + 1;
res = "000" + String(res);
res = "RQ" + res.substring(res.length - 3, res.length);
return res;
}
Related
Is there any way to prevent Apps Script URlFetchApp from creating special characters? For example on a webpage there might be an apostrophe but when I look in the fetched page source that apostrophe would be written as '
Use this decode function to decode your string:
function myFunction(str) {
var str = "'<a>Content © <#>&<&#># </a>"
var decoded = decode(str);
Logger.log(decoded)
}
function decode(str) {
return str.replace(/&#(\d+);/g, function(match, dec) {
return String.fromCharCode(dec);
}).replace(/(&#x(\d+);)/g, function(match, str1) {
var hex = match.toString().match(/(\d+)/g)[0];
var str = '';
for (var n = 0; n < hex.length; n += 2) {
str += String.fromCharCode(parseInt(hex.substr(n, 2), 16));
}
return str;
});
}
This will decode the HTML entities in JavaScript. Combined both my references. Decodes ones with x prepended and the others without.
Reference:
https://stackoverflow.com/a/23270912/16132436
https://www.codegrepper.com/code-examples/javascript/hex+to+ascii+function+javascript
I created a function that iterates by a if statement over a list in order to find a match, when found I wanted to return the match value, but it only happen once, the return statements are at the end of the function and the if statement.
The question is, How can I avoid that this function stops after the first match?, is there another way?, other functions that im not using?
When i run this code I get this:
Anything
Not a match
Not a match
Here is my code:
class Class1(var self: String,var tipo: String,var element: String)
var test_class = Class1("","","")
fun giver(){
test_class.self = "Anything"
test_class.tipo = "Something"
test_class.element = "Nothing"
}
class Funciones(){
fun match_finder(texto: String): Any{
var lista = listOf<String>(test_class.self,test_class.tipo,test_class.element)
var lista_de_listas = listOf<String>("test_class.self","test_class.tipo","test_class.element")
var count = -1
var variable = ""
for (i in lista_de_listas){
count = count + 1
println(count)
if (texto == i){
lista_de_listas = lista
var variable = lista_de_listas[count]
return variable
}
}
return "Not a match"
}
}
fun main(){
giver()
var x = "test_class.self"
var z = "test.class.tipo"
var t = "test.class.element"
var funcion = Funciones()
var y = funcion.match_finder(x)
var c = funcion.match_finder(z)
var r = funcion.match_finder(t)
println(y)
println(c)
println(r)
}
You have some typos in your example. You query for test.class.tipo but in your lista_de_listas you have test_class.tipo with underline. The same is true for test.class.element.
But you should consider to use a Map instead of two list to the lookup:
fun match_finder(texto: String): Any{
val map = mapOf(
"test_class.self" to test_class.self,
"test_class.tipo" to test_class.tipo,
"test_class.element" to test_class.element
)
return map.getOrDefault(texto,"Not a match")
}
I want hash (md5) some txt strings in GAS, and have a problem, may be
incorrect line ending.
Example:
test
test
correct hash 76ce9f441de2ed5de337d391ad4516b7
using GAS i getting wrong hash: e8230113fbba92427c1c41cf34a80c13
function test() {
var data = 'test\
test';
Logger.log(data.MD5());
return (data.MD5());
}
String.prototype.MD5 = function(charset, toByte) {
charset = charset || Utilities.Charset.UTF_8;
var digest = Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, this, charset);
if (toByte) return digest;
var __ = '';
for (i = 0; i < digest.length; i++) {
var byte = digest[i];
if (byte < 0) byte += 256;
var bStr = byte.toString(16);
if (bStr.length == 1) bStr = '0' + bStr;
__ += bStr;
}
return __;
}
As already #Ameen mentioned you are checking different strings
function test(){
var s1 = 'test\ntest';
var s2 = 'test\r\ntest';
Logger.log(s1.MD5() === '76ce9f441de2ed5de337d391ad4516b7');
Logger.log(s2.MD5() === '76ce9f441de2ed5de337d391ad4516b7');
}
[19-03-22 18:03:11:441 MSK] false
[19-03-22 18:03:11:442 MSK] true
A string containing "\r\n" for non-Unix platforms, or a string containing "\n" for Unix platforms.
It seems you're working under Windows.
I have a date like:
19/août/2016 (août = august)
And I have the following function which changes the month into a number:
function swapMonthForNumber(str:String):String {
//do the same line of code for every item in the array
for(var i:int=0;i<months.length;i++){
//i is the item, which is 0 based, so we have to add 1 to make the right month number
str = str.replace(months[i],String(+i+1));
}
//return the updated string
return str;
}
str = swapMonthForNumber(mySharedObject.data.theDate);
trace("Php will use this date :"+str);
So str will be 19/8/2016, but I want str to be 19/08/2016 (adding a 0 before the 8).
How can I do this?
Check out the reference of the Date class!
If forgot to mention this link : flash.globalization.DateTimeFormatter
DateTimeFormatter(requestedLocaleIDName:String, dateStyle:String = "long", timeStyle:String = "long")
Here is an example.
import flash.globalization.DateTimeFormatter;
var df:DateTimeFormatter = new DateTimeFormatter(LocaleID.DEFAULT, DateTimeStyle.SHORT, DateTimeStyle.NONE);
var currentDate:Date = new Date(2016,7,19);
var shortDate:String = df.format(currentDate);
trace (shortDate);
// output : 19/08/2016
DateTimeStyle
LocaleID
Adding leading zeros to a number is commonly called zero padding.
Below is a function to do this, from the answer here.
public function zeroPad(number:int, width:int):String {
var ret:String = ""+number;
while( ret.length < width )
ret="0" + ret;
return ret;
}
In your swapMonthForNumber function, in the for loop, swap the code for this:
var month = zeroPad(i + 1, 2);
str = str.replace(months[i], month);
I have this function:
function tagCheck(tag:String,rez:String):String{
var regExp:RegExp = /\[user_id\](.*?)\[\/user_id\]/g;
var matches:Object = regExp.exec(rez);
return matches[1];
}
I wanna change "user_id" with tag, how can I do that because there are no string in regExp? thank you!
You can create a regex simply by creating a new RegExp class.
public function RegExp(re:String, flags:String)
function tagCheck(tag:String, rez:String) : String {
var tagRegex:String = tag.replace(new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\' + ('\/' || '') + '-]', 'g'), '\\$&');
var regExp:RegExp = new RegExp('\\[' + tagRegex + '\\](.*?)\\[\\/' + tagRegex + '\\]', 'g');
var matches:Object = regExp.exec(rez);
return matches[1];
}
Should work (though untested). The first line is to make sure that a tag called my.tag gets changed into my\.tag.