Download sheets.gs from xiferd/sheets: direct link, hf CLI and curl.
- Browser
- Download file 2.01 kB
-
https://huggingface.co/spaces/xiferd/sheets/resolve/main/sheets.gs
- Command line
-
hf download hf://spaces/xiferd/sheets/sheets.gs
-
curl -L -o sheets.gs https://huggingface.co/spaces/xiferd/sheets/resolve/main/sheets.gs
2.01 kB
| 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); | |
| } | |
| } |