Last active
October 28, 2022 04:50
-
-
Save gangjun06/520582b36ce9771fe4493e491b6a38d0 to your computer and use it in GitHub Desktop.
Export google spread sheet to json ([NovelAI.APP google sheet](https://docs.google.com/spreadsheets/d/18CA__L4yQOs9xAQslP5FUvooD8i8Wz-D7yCnsIYqoBM/edit?usp=sharing)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function exportToJSON() { | |
const result = {} | |
const spreadSheet = SpreadsheetApp.getActiveSpreadsheet().getSheets() | |
spreadSheet.forEach(sheet => { | |
const name = sheet.getName() | |
const matched = name.match(/\[([가-힣!]+)\]/) | |
if(!matched) return | |
result[matched[1]] = [] | |
const num = sheet.getLastRow() | |
const values = sheet.getSheetValues(2,1,num, 5) | |
values.forEach(data => { | |
if(data[0] === "") return | |
result[matched[1]].push({ | |
category: data[0], | |
subCategory: data[1], | |
name: data[2], | |
tags: data[3].split(", "), | |
nsfw: data[4], | |
}) | |
}) | |
}) | |
return JSON.stringify(result) | |
} | |
function onOpen() { | |
var menuEntries = [ {name: "export to json", functionName: "downloadFile"}]; | |
var sheet = SpreadsheetApp.getActiveSpreadsheet(); | |
sheet.addMenu("Utils", menuEntries); | |
} | |
function downloadFile(){ | |
const fileData = exportToJSON() | |
var file = DriveApp.createFile('data.json', fileData); | |
var ui = HtmlService.createHtmlOutput().setTitle('Download'); | |
ui.append(` | |
<div> | |
<style> | |
a { | |
padding: 1rem 2.5rem; | |
background: var(--color-lightGrey); | |
border-radius: 4px; | |
border: 1px solid transparent; | |
font-size: var(--font-size); | |
line-height: 1; | |
text-align: center; | |
-webkit-transition: opacity .2s ease; | |
transition: opacity .2s ease; | |
text-decoration: none; | |
-webkit-transform: scale(1); | |
transform: scale(1); | |
display: inline-block; | |
cursor: pointer; | |
color: #fff; | |
z-index: 1; | |
background-color: #000; | |
background-color: #28bd14; | |
} | |
</style> | |
<a href="${file.getDownloadUrl()}">click to download</a> | |
</div> | |
`) | |
SpreadsheetApp.getActive().show(ui) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment