1
0
Fork 0
This repository has been archived on 2023-12-03. You can view files and clone it, but cannot push or open issues or pull requests.
chellaris-galaxy-political-.../app/api/species/route.ts
2023-06-02 01:41:58 +02:00

88 lines
No EOL
2.5 KiB
TypeScript

import { google } from 'googleapis';
import { NextResponse } from 'next/server';
export async function GET() {
const target = ['https://www.googleapis.com/auth/spreadsheets.readonly'];
const jwt = new google.auth.JWT(
process.env.API_EMAIL,
undefined,
(process.env.API_KEY || '').replace(/\\n/g, '\n'),
target
);
const sheets = google.sheets({ version: 'v4', auth: jwt });
const response = await sheets.spreadsheets.get({
spreadsheetId: process.env.SPREADSHEET_ID,
ranges: [
'Overview!B22:R22',
'Overview!B23:S23',
'Overview!B24:R24',
'Overview!B25:S25',
'Overview!B26:S26',
'Overview!B27:Q27',
'Overview!B28:Q28',
'Overview!B29:Q29',
'Overview!B30:P30',
'Overview!B31:P31',
'Overview!B32:P32',
'Overview!B33:P33',
'Overview!B34:O34',
],
includeGridData: true,
});
const sheetsData = response.data.sheets;
if (sheetsData?.length) {
const rows = sheetsData[0].data;
const merges = sheetsData[0].merges;
if (rows?.length) {
let rowLenghts = new Array<any>;
if (rows[0].rowData?.length) {
rowLenghts = rows[0].rowData;
}
let speciesArray = new Array<number>;
for (let i = 0; i < rows.length; i++) {
let sRows = rows[i].rowData;
if (sRows?.length) {
let data = sRows[0].values;
if (data?.length) {
speciesArray[i] = 0;
for (let j = 0; j < data.length; j++) {
switch (data[j].userEnteredValue?.stringValue) {
case ("Taken"): {
speciesArray[i] = speciesArray[i] + 2;
break;
}
case ("A"):
case ("B"):
case ("-"): {
speciesArray[i] = speciesArray[i] + 1;
break;
}
}
}
if (merges?.length) {
merges.forEach(merge => {
if (merge.startRowIndex && merge.startRowIndex - 21 == i) {
speciesArray[i] = speciesArray[i] + 2;
}
});
}
}
else {
speciesArray[i] = 0;
}
}
}
console.log("New Google Data loaded " + Intl.DateTimeFormat('en-UK', {hour: 'numeric', minute: 'numeric', hour12: false}).format(Date.now()));
return NextResponse.json({ speciesArray });
}
}
else {
return NextResponse.json({ status: 500 })
}
}