sheets / sheets.gs
xiferd's picture
add html frontend, google sheets apps script backend, local webserver cmd, fix gz file type in git
f7f8cfe
const HF_TOKEN = "hf_"; // Get this from hf.co/settings/tokens (Write access)
const REPO_ID = "you/space";
const FILENAME = "data.json.gz"
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu('HF')
.addItem('Deploy', 'pushToHuggingFace')
.addToUi();
}
function pushToHuggingFace() {
const sheet = SpreadsheetApp.getActiveSheet();
const jsonString = JSON.stringify(sheet.getDataRange().getValues());
// 2. Gzip the JSON and Base64 encode it
const blob = Utilities.newBlob(jsonString, 'application/json', 'data.json');
const gzippedBlob = Utilities.gzip(blob, FILENAME);
const base64Content = Utilities.base64Encode(gzippedBlob.getBytes());
// 3. Prepare the Commit Payload
// Use /api/spaces/REPO_ID/commit/main
const url = `https://huggingface.co/api/spaces/${REPO_ID}/commit/main`;
const payload = {
"summary": `Update ${FILENAME} from Google Sheets`,
"files": [
{
"path": FILENAME,
"content": base64Content,
"encoding": "base64"
}
]
};
const options = {
"method": "POST",
"headers": {
"Authorization": `Bearer ${HF_TOKEN}`,
"Content-Type": "application/json"
},
"payload": JSON.stringify(payload),
"muteHttpExceptions": true // Allows us to see the error message if it fails
};
// 4. Send the Commit
try {
const response = UrlFetchApp.fetch(url, options);
const code = response.getResponseCode();
const result = response.getContentText();
if (code === 200 || code === 201) {
Logger.log("Successfully committed: " + result);
SpreadsheetApp.getUi().alert(`βœ… ${REPO_ID} updated!`);
} else {
Logger.log("Failed with code " + code + ": " + result);
SpreadsheetApp.getUi().alert("❌ Error: " + result);
}
} catch (e) {
Logger.log("Script Error: " + e.toString());
SpreadsheetApp.getUi().alert("❌ Script Exception: " + e.message);
}
}