|
| 1 | +import { getProject } from './lib/project.mjs'; |
| 2 | +import reportError from './lib/report-error.mjs'; |
| 3 | +import { fetchMapping } from './lib/w3cid-map.mjs'; |
| 4 | +import { convertProjectToHTML } from '../common/project2html.mjs'; |
| 5 | + |
| 6 | + |
| 7 | +/** |
| 8 | + * Generate the grid for the current spreadsheet |
| 9 | + */ |
| 10 | +export default function () { |
| 11 | + return exportEventToFiles(SpreadsheetApp.getActiveSpreadsheet()); |
| 12 | +} |
| 13 | + |
| 14 | + |
| 15 | +/** |
| 16 | + * Generate the grid in the provided spreadsheet |
| 17 | + */ |
| 18 | +async function exportEventToFiles(spreadsheet) { |
| 19 | + try { |
| 20 | + console.log('Read data from spreadsheet...'); |
| 21 | + const project = getProject(spreadsheet); |
| 22 | + project.w3cIds = await fetchMapping(); |
| 23 | + if (!project.sheets.sessions.sheet) { |
| 24 | + reportError('No sheet found that contains the list of sessions, please import data from GitHub first.'); |
| 25 | + return; |
| 26 | + } |
| 27 | + console.log('Read data from spreadsheet... done'); |
| 28 | + |
| 29 | + console.log('Convert to HTML...'); |
| 30 | + const html = await convertProjectToHTML(project); |
| 31 | + console.log('Convert to HTML... done'); |
| 32 | + |
| 33 | + console.log('Create data URL for the HTML export...'); |
| 34 | + const htmlBase64 = Utilities.base64Encode(html, Utilities.Charset.UTF_8); |
| 35 | + const htmlDataUrl = `data:text/html;charset=UTF8;base64,${htmlBase64}`; |
| 36 | + console.log('Create data URL for the HTML export... done'); |
| 37 | + |
| 38 | + console.log('Create data URL for the JSON export...'); |
| 39 | + const jsonBase64 = Utilities.base64Encode( |
| 40 | + JSON.stringify(project, null, 2), |
| 41 | + Utilities.Charset.UTF_8); |
| 42 | + const jsonDataUrl = `data:application/json;charset=UTF8;base64,${jsonBase64}`; |
| 43 | + console.log('Create data URL for the JSON export... done'); |
| 44 | + |
| 45 | + console.log('Report result...'); |
| 46 | + const htmlOutput = HtmlService |
| 47 | + .createHtmlOutput(`<p> |
| 48 | + Event data files prepared. |
| 49 | + Click following link(s) to download them: |
| 50 | + </p> |
| 51 | + <ul> |
| 52 | + <li> |
| 53 | + <a href="${htmlDataUrl}" download="event.html"> |
| 54 | + Download event schedule as an HTML file |
| 55 | + </a> |
| 56 | + </li> |
| 57 | + <li> |
| 58 | + <a href="${jsonDataUrl}" download="event.json"> |
| 59 | + Download event data as a JSON file |
| 60 | + </a> |
| 61 | + </li> |
| 62 | + </ul> |
| 63 | + `) |
| 64 | + .setWidth(300) |
| 65 | + .setHeight(400); |
| 66 | + SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Export event to a file'); |
| 67 | + console.log('Report result... done'); |
| 68 | + } |
| 69 | + catch(err) { |
| 70 | + reportError(err.toString()); |
| 71 | + return; |
| 72 | + } |
| 73 | +} |
0 commit comments