Files
rural-water-workorder/google-script.js
ninjasurge 4e763305fb Update google-script.js
This now matches the last production version that was in use.
2025-04-02 09:29:08 -05:00

97 lines
2.8 KiB
JavaScript

/**
* # TODO
* - Rename the Rescan button Button
* - Add support to edit old workorders
* - Error Handling
*/
// Setup sheetdb
SpreadsheetApp.enableBigQueryExecution();
// let dbURL = 'https://sheetdb.io/api/v1/3odkwkhm12mmq' // Test Sheet
let dbURL = 'https://sheetdb.io/api/v1/94onb56xn1grs?sheet=Workorder%20Tracker' // Prod
let dbAuth = 'q1nwowwf52hf3aty57x88iujcmw9en1yq0480sdr'
let getWorkorderSheet = UrlFetchApp.fetch(dbURL, {
method:'GET',
headers: {'Authorization': `Bearer ${dbAuth}`}
})
// Serve the Index.html file
function doGet() {
return HtmlService.createHtmlOutputFromFile('Index');
}
// Recieve data from Index.html file
function scanResults(qrcode) {
let items = []
qrcode.workorderInventory.forEach( item => {
let info = {
Timestamp: "DATETIME",
"Workorder Number": qrcode.workorderNumber,
"Item Number": item[0],
Amount: item[1],
// "Email Address": "NA",
"Customer Information": qrcode.customerName,
// "Item Used": "",
"Tap Number": qrcode.tapNumber,
"Work Description": qrcode.workDescription,
"Customer Leak Reward?": "",
"Work Notes": qrcode.workNotes,
// "Time spent": "",
"Completed By": qrcode.employees,
"Meter Reading": qrcode.meterReading,
"Meter Number": qrcode.meterSerial,
// "Item Amount": "",
// COST: "",
}
items.push(info)
})
create(items)
}
// Create Row on workorder table
function create(row) {
return UrlFetchApp.fetch(dbURL, {
method:'post',
contentType: 'application/json',
headers: {
'Authorization': `Bearer ${dbAuth}`,
'Accept': 'application/json',
},
payload: JSON.stringify({
data: row
})
})
}
// Legacy Code
// function createOld(timestamp = "", workorderNumber = '00100', itemNumeber = 0, amount = 0, email = 'john@example.com', customerInfo = "NA", itemUsed = "NA", tapNumber = 0, description = "NA", leakReward = false, workNote = "NA", timeSpent = 0, completedBy = "NA", meterReading = "010", meterNumber = 0, itemAmount = 0, cost = 0) {
// return UrlFetchApp.fetch(dbURL, {
// method:'post',
// contentType: 'application/json',
// headers: {
// 'Authorization': `Bearer ${dbAuth}`,
// 'Accept': 'application/json',
// },
// payload: JSON.stringify({
// data: [{
// Timestamp: timestamp,
// "Workorder Number": workorderNumber,
// "Item Number": 0,
// Amount: 0,
// "Email Address": "",
// "Customer Information": "",
// "Item Used": "",
// "Tap Number": "",
// "Work Description": "",
// "Customer Leak Reward?": "",
// "Work Notes": "",
// "Time spent": "",
// "Completed By": "",
// "Meter Reading": "",
// "Meter Number": "",
// "Item Amount": "",
// COST: "",
// }]
// })
// })
// }