forked from SRCPloetoe/projectnextech
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
58 lines (51 loc) · 1.99 KB
/
script.js
File metadata and controls
58 lines (51 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
let sheetDataCache = {};
async function fetchEntireSheet(sheetName) {
try {
const response = await fetch(`https://sheets.googleapis.com/v4/spreadsheets/1LcLawIQ8Fg-p2LyErc3qZaKSPL1xC26IJ73LUhLfMgI/values/${sheetName}?key=AIzaSyBw_tO848SgZDJeoZwGr10lUb5w3ks0H4U`);
const data = await response.json();
if (data.values && data.values.length > 0) {
// Store data in a global cache
sheetDataCache[sheetName] = data.values;
} else {
throw new Error('No data found.');
}
} catch (error) {
console.error('Error fetching entire sheet:', error);
}
}
function getCachedSheetData(sheetName, cellRange) {
const rangeMatch = cellRange.match(/^([A-Z]+)([0-9]+)$/);
if (!rangeMatch) {
throw new Error('Invalid cell range format.');
}
const col = rangeMatch[1];
const row = parseInt(rangeMatch[2], 10);
const colIndex = col.charCodeAt(0) - 'A'.charCodeAt(0);
const rowIndex = row - 1;
if (sheetDataCache[sheetName] && sheetDataCache[sheetName][rowIndex]) {
return sheetDataCache[sheetName][rowIndex][colIndex];
} else {
throw new Error('Data not found in cache.');
}
}
async function displaySheetData(sheetName, cellRange, elementId) {
try {
const cellValue = getCachedSheetData(sheetName, cellRange);
const element = document.getElementById(elementId);
if (element) {
if (cellValue.slice(-4) == ".png" || cellValue.slice(-4) == ".jpg" || cellValue.slice(-5) == ".jpeg") {
element.src = cellValue;
} else {
element.innerHTML = cellValue.replace(/\n/g, '<br>');
}
} else {
console.error(`Element with id ${elementId} not found.`);
}
} catch (error) {
console.error('Error displaying data:', error);
const element = document.getElementById(elementId);
if (element) {
element.innerHTML = 'Error loading data.';
}
}
}