Skip to content

Commit

Permalink
testing penyedia terumumkan
Browse files Browse the repository at this point in the history
  • Loading branch information
deniganda committed Aug 14, 2024
1 parent 8368162 commit 77b3164
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
9 changes: 8 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ jobs:

- name: Install dependencies
run: npm install


- name: Run Penyedia Node.js script
run: node Penyedia_Terumumkan.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 }}
SPREADSHEET_ID: ${{ secrets.SPREADSHEET_ID }}

- name: Run Tender Selesai Node.js script
run: node Tender_Selesai.js # Replace with the path to your first script
env:
Expand Down
65 changes: 65 additions & 0 deletions Penyedia_Terumumkan.js
Original file line number Diff line number Diff line change
@@ -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 = 'Penyedia!A:ZZ'; // Range to clear
const updateRange = 'Penyedia!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();

0 comments on commit 77b3164

Please sign in to comment.