992
edits
(change to code tag) |
(how to update a google sheet cell with numbers from a github csv file) |
||
| Line 13: | Line 13: | ||
**[[Thunderbird/Support/TB115.0SupportIssues]] | **[[Thunderbird/Support/TB115.0SupportIssues]] | ||
== 2026-01-15 How To update a google sheet automatically from a CSV on github == | |||
from a google search for <code>how to update a google sheet cell with numbers from a github csv file</code>: | |||
'''Method 1: Using the <code>IMPORTDATA</code> Function (Manual/Semi-automated)''' | |||
This method is the simplest if your CSV file is publicly accessible and you<br /> | |||
don’t need real-time, automatic updates. The data will refresh<br /> | |||
periodically or when the sheet is manually reopened.<br /> | |||
* 1. '''Get the raw CSV URL from GitHub:''' Navigate to your CSV file on GitHub, click the '''“Raw”''' button, and copy the URL from your browser’s address bar. This URL<br /> | |||
should point directly to the raw data and typically starts with <code>raw.githubusercontent.com...</code>.<br /> | |||
* 2. '''Use the <code>IMPORTDATA</code> function:''' In the desired Google Sheets cell (e.g., A1), enter the following formula, replacing <code>"YOUR_RAW_CSV_URL"</code> with the URL you copied: | |||
<pre class="excel">=IMPORTDATA("YOUR_RAW_CSV_URL")</pre> | |||
* 3. '''Press Enter:''' The data from the GitHub CSV file will populate the sheet starting from<br /> | |||
that cell. The data will automatically arrange itself into rows and<br /> | |||
columns | |||
'''Method 2: Using Google Apps Script (Automated/Programmatic)''' | |||
For more robust solutions, such as updating a specific range of cells or<br /> | |||
scheduling automatic updates, Google Apps Script is the best approach. | |||
* 1. '''Open the Apps Script editor:''' In your Google Sheet, go to '''Extensions''' > '''Apps Script'''. | |||
* 2. '''Paste the script:''' Delete any existing code in the <code>Code.gs</code> file and paste the following script. Remember to replace <code>"YOUR_RAW_CSV_URL"</code> with your actual GitHub raw CSV URL and <code>"Sheet1"</code> with your target sheet’s name: | |||
javascript | |||
<syntaxhighlight lang="js">function importCSVFromGitHub() { | |||
const url = "YOUR_RAW_CSV_URL"; // Replace with your GitHub raw CSV URL | |||
const sheetName = "Sheet1"; // Replace with your target sheet name | |||
const targetCell = "A1"; // The cell to start pasting data | |||
try { | |||
// Fetch the CSV data | |||
const response = UrlFetchApp.fetch(url); | |||
const csvData = response.getContentText(); | |||
// Parse the CSV data into a 2D array | |||
const dataArray = Utilities.parseCsv(csvData); | |||
// Get the target sheet and range | |||
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName); | |||
if (!sheet) { | |||
Logger.log("Sheet not found: " + sheetName); | |||
return; | |||
} | |||
// Update the sheet with the new data | |||
// Clear previous data in the target range first if needed | |||
const startRange = sheet.getRange(targetCell); | |||
const startRow = startRange.getRow(); | |||
const startColumn = startRange.getColumn(); | |||
// Clear only the area the new data will occupy | |||
sheet.getRange(startRow, startColumn, sheet.getLastRow(), sheet.getLastColumn()).clearContent(); | |||
// Set the new values | |||
sheet.getRange(startRow, startColumn, dataArray.length, dataArray[0].length).setValues(dataArray); | |||
Logger.log("CSV data successfully imported to " + sheetName); | |||
} catch (e) { | |||
Logger.log("Error importing CSV: " + e.toString()); | |||
} | |||
}</syntaxhighlight> | |||
* 3. '''Save and run the script:''' | |||
** Click the '''Save''' icon (floppy disk) and name your project. | |||
** In the function dropdown menu (usually showing <code>importCSVFromGitHub</code> or <code>myFunction</code>), ensure <code>importCSVFromGitHub</code> is selected. | |||
** Click the '''Run''' button (play icon). | |||
** Authorize** the script when prompted by following the on-screen instructions. | |||
* 4. '''Schedule automatic updates (optional):''' You can set a time-driven trigger to run this script automatically at specific intervals: | |||
** In the Apps Script editor, click the '''Triggers''' icon (alarm clock). | |||
** Click '''+ Add Trigger'''. | |||
** In the “Choose which function to run” dropdown, select <code>importCSVFromGitHub</code>. | |||
** Under “Select event source”, choose '''Time-driven'''. | |||
** Configure the frequency (e.g., every hour, day) and save the trigger | |||
== 2026-01-15 How to syntax colour and highligt aka escape JavaScript code in wikitext== | == 2026-01-15 How to syntax colour and highligt aka escape JavaScript code in wikitext== | ||
* apparently it's <code><syntaxhighlight lang="javascript">your JavaScript code here</syntaxhighlight></code>. For example: | * apparently it's <code><syntaxhighlight lang="javascript">your JavaScript code here</syntaxhighlight></code>. For example: | ||
edits