do what we can
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
// Minimal add-in taskpane script — one button that calls the playground server
|
||||
// Wait for Office to be ready before attaching event handlers
|
||||
// Minimal add-in taskpane script — reads Excel data and uploads to playground server
|
||||
Office.onReady(function(info) {
|
||||
// info.host will be 'Excel' when running in Excel
|
||||
console.log('Office.onReady fired, host:', info.host);
|
||||
|
||||
const uploadBtn = document.getElementById('uploadBtn');
|
||||
@@ -11,15 +9,55 @@ Office.onReady(function(info) {
|
||||
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('Starting upload...');
|
||||
setStatus('Reading Excel data...');
|
||||
try {
|
||||
const res = await fetch('http://localhost:3002/api/upload-managers', { method: 'POST' });
|
||||
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: ' + link, true);
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user