Google Developer forums

Sequential appscript launch

An idea for sequential data entry in Google Sheets; if it were a database, I would use auto-increment.

function sequenciarOrdemServico() {

const lock = LockService.getScriptLock();

lock.waitLock(30000); // trava até 30s

try {

const props = PropertiesService.getScriptProperties();

let seq = Number(props.getProperty('sequencia')) || 0;



const ss = SpreadsheetApp.getActiveSpreadsheet();

const sheet = ss.getSheetByName('table'); // 🔴 ajuste o nome

const data = sheet.getDataRange().getValues();



const header = data\[0\];

const colNumOS = header.indexOf('sequencia');

const colId = header.indexOf('id');



if (colNumOS === -1) throw 'Coluna NUM_OS não encontrada';



for (let i = 1; i < data.length; i++) {

  if (!data\[i\]\[colNumOS\]) {

    seq++;

    sheet.getRange(i + 1, colNumOS + 1).setValue(seq);

  }

}



props.setProperty('sequencia', seq);

} finally {

lock.releaseLock();

}

}

I used this to reset the records, because even after deleting the line, the sequence continues from the last entry.

function resetarSequenciaOS() {
const props = PropertiesService.getScriptProperties();
props.deleteProperty(‘sequencia’);
}

1 Like
Morty Proxy This is a proxified and sanitized view of the page, visit original site.