Propuesta:
Script para crear un id único ascendente. su función es crear un índice de incremento para utilizarlo como Id único.
llamada función diferentes Key:
getId(1) retorno myId1_1
getId(1) retorno myId1_2
. . . etc.
getId(2) retorno myId2_1
getId(2) retorno myId2_2
. . . etc.
get(1,-1) retorno 0
get(2,-1) retorno 0
Código.gs
/* see @use JSDoc:
https://jsdoc.app/about-getting-started */
/**
* Ged number id call each.
* @param {integer} key number Key >=0.
* @param {number} deleteKey (Optional) delete key -1 or not 0, defaulst 0.
* @returns {string} id unique.
*/
function getId(key, deleteKey = 0) {
if (!Number.isInteger(key))
throw TypeError(`param{1} key = ${key} not number integer`); /* End if */
if (key < 0)
throw RangeError(`param{1} key = ${key} not >= 0`); /* End if */
if (!Number.isInteger(deleteKey))
throw TypeError(`param{2} key = ${key} not number integer`); /* End if */
if (deleteKey !== 0 && deleteKey !== -1)
throw RangeError(`param{2} key = ${key} not -1 or 0`); /* End if */
/**
* Get Class Properties.
* @returns {class} methods and properties.
* @see Clas Properties:
* https://developers.google.com/apps-script/reference/properties/properties?hl=es-419
*/
const classProperties = PropertiesService.getDocumentProperties();
/**
* set value name key
* @returns {string} initiales 'myId' concatenate key
*/
const myKey = `myId${key}`;
/**
* Get Methodos Class Lock
* @returns {class} Methos Class Lock
* @see Class Lock:
* https://developers.google.com/apps-script/reference/lock/lock
*/
const classLock = LockService.getDocumentLock();
/*
Waits for up to 30 seconds for other processes to finish.
see waitLock(timeoutInMillis):
https://developers.google.com/apps-script/reference/lock/lock
*/
classLock.waitLock(30000); // 30 seconds.
if (deleteKey) {
/* delete key */
classProperties.deleteProperty(myKey); result = 0;
}
else {
/**
* Get value key Id.
* @returns {string} get value or 0 is null.
* @see Method getProperty(string):
* https://developers.google.com/apps-script/reference/properties/properties?hl=es-419#getpropertykey
*/
var result = classProperties.getProperty(myKey || 0);
/* increment id or one */
result++;
/* set store not exist create value one */
classProperties.setProperty(myKey, result);
}; /* End if else */
/*
Releases the lock, allowing other processes waiting on the lock to continue.
returns {error} Not success exception error.
@see Method releaseLock():
https://developers.google.com/apps-script/reference/lock/lock?hl=es-419#releaselock
*/
classLock.releaseLock();
/* concatenate unique id different key */
result =`${myKey}_${result}`;
/*
see: Class console
https://developers.google.com/apps-script/reference/base/console
*/
console.info('function geId(key,deleteKey) key =', myKey, ' value =', result, 'deleteKey =', Boolean(deleteKey));
return result;
}; /* End function getId(Key) */
Instruccion:
hoja.getRange(ultimaFila, 1).setValue(getId(1));
Referencias:
Class PropertiesService:
Permite que las secuencias de comandos almacenen datos simples en pares clave-valor.
Método setProperty(key, value):
Establece el par clave-valor determinado en el almacén.
getProperty(key):
Obtiene el valor asociado con la clave dada.
Class Lock:
Esta clase permite que las secuencias de comandos se aseguren de que solo una instancia de la secuencia de comandos ejecute una sección determinada de código a la vez
JSDoc:
/** comentarios */ Si seleccionas las variable y tecleas Ctrl+Espacio se muestra una ventana emergente con el comentario el link de ayuda.