in marketing-analytics/activation/sheets-based-installer/src/apps_script/6_sheet/colab_solution_sheet.js [251:330]
loadColabToSheet_(inputUrl) {
const ui = SpreadsheetApp.getUi();
let colabUrl;
let fileId;
const regex = /^(https:\/\/)?colab.\w*.google.com\/([^/]*)\/([^#?]*)/g;
const matches = regex.exec(inputUrl);
if (matches) {
const resourceId = matches[3];
switch (matches[2]) {
case 'github':
const githubLink = resourceId.replace('/blob/', '/');
colabUrl = `https://raw.githubusercontent.com/${githubLink}`;
break;
case 'drive':
fileId = resourceId;
break;
default:
ui.alert(`Unknown Colab file source: ${inputUrl}`);
return;
}
} else {
fileId = inputUrl;
}
let colab;
if (fileId) {
colab = new Drive().getFileContent(fileId);
} else if (colabUrl) {
colab = JSON.parse(UrlFetchApp.fetch(colabUrl).getContentText());
}
if (!colab || !colab.cells) {
console.log('Colab', colab);
ui.alert(`Unknown Colab content from: ${inputUrl}`);
return;
}
const data = [];
if (fileId) {
const { name } = new Drive().getFileInfo(fileId);
data.push([`Colab: ${name}`, inputUrl]);
} else {
data.push([`Colab: ${colab.metadata.colab.name}`, inputUrl]);
}
const textIndex = this.fields.indexOf('text');
const codeIndex = this.fields.indexOf('code');
const metadataIdIndex = this.fields.indexOf('metadataId');
let currentRow;
colab.cells.forEach(({ cell_type, source, metadata: { id } }) => {
if (cell_type === 'markdown') {
currentRow = [];
currentRow[textIndex] = source.join('');
data.push(currentRow);
} else if (cell_type === 'code') {
const codeRow = currentRow ? currentRow : [];
codeRow[codeIndex] = source.join('');
codeRow[metadataIdIndex] = id;
if (this.legacyConfig[id]) {
const legacy = this.legacyConfig[id];
Object.keys(legacy).forEach((key) => {
codeRow[this.fields.indexOf(key)] = legacy[key];
});
console.log('got legacy for', id, codeRow);
}
if (currentRow) {
currentRow = undefined;
} else {
data.push(codeRow);
}
}
});
data.forEach((row) => {
row[this.fields.length - 1] = row[this.fields.length - 1] || '';
});
this.data = data;
this.getEnhancedSheet().initialize(this);
SpreadsheetApp.flush();
SpreadsheetApp.getActiveSpreadsheet()
.toast(`Colab content has been loaded from: ${inputUrl}`);
}