69 lines
2.6 KiB
JavaScript
69 lines
2.6 KiB
JavaScript
// Minimal add-in taskpane script — reads Excel data and uploads to playground server
|
|
Office.onReady(function(info) {
|
|
console.log('Office.onReady fired, host:', info.host);
|
|
|
|
const uploadBtn = document.getElementById('uploadBtn');
|
|
const status = document.getElementById('status');
|
|
|
|
function setStatus(text, ok) {
|
|
status.innerHTML = '<div class="' + (ok ? 'ok' : 'err') + '">' + text + '</div>';
|
|
}
|
|
|
|
// Read data from worksheet "Managers Info" and convert A1:E49 into array of objects
|
|
// Only rows with any non-empty cell are included; first row is header
|
|
async function getExcelData() {
|
|
return Excel.run(async (context) => {
|
|
// Use the specific worksheet by name and the fixed range A1:E49
|
|
const sheet = context.workbook.worksheets.getItem('Managers Info');
|
|
const range = sheet.getRange('a2:E49');
|
|
range.load('values');
|
|
await context.sync();
|
|
|
|
const values = range.values;
|
|
if (!values || values.length < 2) {
|
|
throw new Error('No data found in Managers Info A1:E49 (need header row + at least one data row)');
|
|
}
|
|
|
|
const headers = values[0].map(h => String(h || '').trim());
|
|
const records = [];
|
|
for (let i = 1; i < values.length; i++) {
|
|
const row = values[i];
|
|
// skip fully-empty rows
|
|
const isEmpty = row.every(c => c === null || c === undefined || String(c).trim() === '');
|
|
if (isEmpty) continue;
|
|
const obj = {};
|
|
headers.forEach((h, idx) => {
|
|
obj[h] = row[idx];
|
|
});
|
|
records.push(obj);
|
|
}
|
|
if (records.length === 0) throw new Error('No data rows found in Managers Info A1:E49');
|
|
return records;
|
|
});
|
|
}
|
|
|
|
uploadBtn.addEventListener('click', async function() {
|
|
uploadBtn.disabled = true;
|
|
setStatus('Reading Excel data...');
|
|
try {
|
|
const records = await getExcelData();
|
|
setStatus('Uploading ' + records.length + ' rows...');
|
|
|
|
const res = await fetch('http://localhost:3002/api/upload-managers', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ records: records })
|
|
});
|
|
const json = await res.json();
|
|
if (!res.ok) throw new Error(json && json.error ? json.error : 'Unknown response');
|
|
var link = (json.result && json.result.share && json.result.share.link) ? json.result.share.link.webUrl : 'unknown';
|
|
setStatus('Upload finished. Share link: <a href="' + link + '" target="_blank">' + link + '</a>', true);
|
|
} catch (err) {
|
|
console.error(err);
|
|
setStatus('Upload failed: ' + (err.message || err), false);
|
|
} finally {
|
|
uploadBtn.disabled = false;
|
|
}
|
|
});
|
|
});
|