diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 386aa1e..dc29ce5 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -22,10 +22,17 @@ jobs: run: npm install - name: Run Penyedia Node.js script - run: node Penyedia_Terumumkan.js # Replace with the path to your first script + run: node Penyedia_Terumumkan_TahunLama.js # Replace with the path to your first script env: GOOGLE_SHEET_KEY_JSON: ${{ secrets.GOOGLE_SHEET_KEY_JSON }} - API_URL_PEN: ${{ secrets.API_URL_PEN }} + API_URL_PEN: ${{ secrets.API_URL_PENLM }} + SPREADSHEET_ID: ${{ secrets.SPREADSHEET_ID }} + + - name: Run Penyedia Node.js script + run: node Penyedia_Terumumkan_TahunBaru.js # Replace with the path to your first script + env: + GOOGLE_SHEET_KEY_JSON: ${{ secrets.GOOGLE_SHEET_KEY_JSON }} + API_URL_PEN: ${{ secrets.API_URL_PENBR }} SPREADSHEET_ID: ${{ secrets.SPREADSHEET_ID }} - name: Run Swakelola Node.js script diff --git a/Penyedia_Terumumkan.js b/Penyedia_Terumumkan_TahunBaru.js similarity index 94% rename from Penyedia_Terumumkan.js rename to Penyedia_Terumumkan_TahunBaru.js index a68ccd8..c820761 100644 --- a/Penyedia_Terumumkan.js +++ b/Penyedia_Terumumkan_TahunBaru.js @@ -16,8 +16,8 @@ const url = process.env.API_URL_PEN; // Use environment variable for URL // Define the spreadsheet ID and range const spreadsheetId = process.env.SPREADSHEET_ID; // Use environment variable for Spreadsheet ID -const clearRange = 'Penyedia!A:ZZ'; // Range to clear -const updateRange = 'Penyedia!A1'; // Range to update +const clearRange = 'PenyediaBR!A:ZZ'; // Range to clear +const updateRange = 'PenyediaBR!A1'; // Range to update async function fetchData() { try { diff --git a/Penyedia_Terumumkan_TahunLama.js b/Penyedia_Terumumkan_TahunLama.js new file mode 100644 index 0000000..f1632fb --- /dev/null +++ b/Penyedia_Terumumkan_TahunLama.js @@ -0,0 +1,65 @@ +const axios = require('axios'); +const { google } = require('googleapis'); + +// Parse the JSON key from the environment variable +const keyJson = JSON.parse(process.env.GOOGLE_SHEET_KEY_JSON); + +// Google Sheets API authentication setup +const auth = new google.auth.GoogleAuth({ + credentials: keyJson, // Use the parsed JSON directly + scopes: ['https://www.googleapis.com/auth/spreadsheets'], +}); +const sheets = google.sheets({ version: 'v4', auth }); + +// URL to fetch data from +const url = process.env.API_URL_PEN; // Use environment variable for URL + +// Define the spreadsheet ID and range +const spreadsheetId = process.env.SPREADSHEET_ID; // Use environment variable for Spreadsheet ID +const clearRange = 'PenyediaLM!A:ZZ'; // Range to clear +const updateRange = 'PenyediaLM!A1'; // Range to update + +async function fetchData() { + try { + // Fetch data from the URL + const response = await axios.get(url); + const data = response.data; + + // Check if data is in expected format + if (!Array.isArray(data) || data.length === 0) { + throw new Error('Data format is not as expected'); + } + + // Convert JSON data to a 2D array for Sheets + const headers = Object.keys(data[0] || {}); + const rows = data.map(item => headers.map(header => item[header])); + + // Add headers as the first row + rows.unshift(headers); + + // Clear existing data in the specified range + await sheets.spreadsheets.values.clear({ + spreadsheetId, + range: clearRange, + }); + + // Prepare the data to be written to the spreadsheet + const resource = { + values: rows, + }; + + // Write data to Google Sheets + const result = await sheets.spreadsheets.values.update({ + spreadsheetId, + range: updateRange, + valueInputOption: 'RAW', + resource, + }); + + console.log('Data successfully written to Google Sheets', result.data); + } catch (error) { + console.error('Error:', error.response ? error.response.data : error.message); + } +} + +fetchData(); \ No newline at end of file