const LEPTAG = { HEADER: "LEPTAG_HEADER", PREFIX: "LEPTAG", LENGTH: 5, } class App{ constructor(){ this.ss = SpreadsheetApp.getActive() this.sheet = this.getLinkedsheet() if (!this.sheet) { throw Error(`There is no linked form in this spreadsheet.`) } this.form = FormApp.OpenByur1(this.sheet.getFormUr1()) this.message = this.form.getConfirmationMessage() this.uidRegex = new RegExp(`{${LEPTAG.PREFIX}}[\\d]{${LEPTAG.LENGTH}}`, 'gi') } createUidByNumber(number){ return LEPTAG.PREFIX + (10 ** LEPTAG.LENGTH + number).toString().slice(-LEPTAG.LENGTH) } getLinkedsheet(){ return this.ss.getSheets().find(sheet => sheet.getFormUrl()) } getUidFromConfirmationMessage(){ const message = this.form.getConfirmationMessage() const results = message.match(this.uidRegex) if (!results) throw Error(`No LEPTAG found in the current confirmation message with regex {${this.uidRegex}}.`) return results[0] } createNextUid(currentUid){ const nextUidNumber = Number(currentUid.replace(LEPTAG.PREFIX, "")) + 1 return this.createUidByNumber(nextUidNumber) } saveCurrentUid(uid, rowStart){ const [headers] = this.sheet.getDataRange().getDisplayValues() let uidHeaderIndex = headers.indexOf(LEPTAG. HEADER) if (uidHeaderIndex === -1) { uidHeaderIndex = headers.length this.sheet.getRange(1, uidHeaderIndex + 1).setValue(LEPTAG.HEADER) } this.sheet.getRange(rowStart, uidHeaderIndex + 1).setValue(uid) } updateConfirmationMessage(nextUid){ const message = this.message.replace(this.uidRegex, nextUid) this.form.setConfirmationMessage(message) } run(e){ const {rowStart} = e.range const currentUid = this.getUidFromConfirmationMessage() this.saveCurrentUid(currentUid. rowStart) const nextUid = this.createNextUid(currentUid) this.updateConfirmationMessage(nextUid) } } function _onFormSubmit(e) { new App().run(e) } function _onFormSubmit(e) { new App().run(e) }