mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-20 22:49:18 +00:00
use exist function befor pull or push
This commit is contained in:
@@ -25,9 +25,6 @@ class sheet {
|
|||||||
private readonly clientSecretFile: string;
|
private readonly clientSecretFile: string;
|
||||||
private static clientSecret = null;
|
private static clientSecret = null;
|
||||||
private static authUrl: null | string = null;
|
private static authUrl: null | string = null;
|
||||||
private worksheetId = 0;
|
|
||||||
private sheetId = '';
|
|
||||||
private range = '';
|
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
const appDataPath = getAppDataPath();
|
const appDataPath = getAppDataPath();
|
||||||
@@ -45,7 +42,6 @@ class sheet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async getSheetState(): Promise<GoogleSheetState> {
|
public async getSheetState(): Promise<GoogleSheetState> {
|
||||||
console.log('getSheetState');
|
|
||||||
const ret: GoogleSheetState = {
|
const ret: GoogleSheetState = {
|
||||||
secret: false,
|
secret: false,
|
||||||
auth: false,
|
auth: false,
|
||||||
@@ -53,9 +49,6 @@ class sheet {
|
|||||||
worksheet: false,
|
worksheet: false,
|
||||||
worksheetOptions: [],
|
worksheetOptions: [],
|
||||||
};
|
};
|
||||||
const lastID = this.sheetId;
|
|
||||||
this.sheetId = '';
|
|
||||||
this.worksheetId = 0;
|
|
||||||
|
|
||||||
if (!sheet.clientSecret) {
|
if (!sheet.clientSecret) {
|
||||||
return ret;
|
return ret;
|
||||||
@@ -73,7 +66,7 @@ class sheet {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
const settings = DataProvider.getGoogleSheet();
|
const settings = DataProvider.getGoogleSheet();
|
||||||
if (settings.id != '' && lastID != settings.id) {
|
if (settings.id != '') {
|
||||||
const spreadsheets = await sheets({ version: 'v4', auth: sheet.client })
|
const spreadsheets = await sheets({ version: 'v4', auth: sheet.client })
|
||||||
.spreadsheets.get({
|
.spreadsheets.get({
|
||||||
spreadsheetId: settings.id,
|
spreadsheetId: settings.id,
|
||||||
@@ -92,20 +85,6 @@ class sheet {
|
|||||||
}
|
}
|
||||||
ret.worksheet = true;
|
ret.worksheet = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// const x = await this.exist(settings.id, settings.worksheet);
|
|
||||||
// if (x === true) {
|
|
||||||
// ret.id = true;
|
|
||||||
// this.sheetId = settings.id;
|
|
||||||
// } else if (x !== false) {
|
|
||||||
// ret.id = true;
|
|
||||||
// ret.worksheet = true;
|
|
||||||
// this.sheetId = settings.id;
|
|
||||||
// this.worksheetId = x.worksheetId;
|
|
||||||
// this.range = x.range;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,13 +92,10 @@ class sheet {
|
|||||||
* test existence of sheet and worksheet
|
* test existence of sheet and worksheet
|
||||||
* @param {string} sheetId - https://docs.google.com/spreadsheets/d/[[spreadsheetId]]/edit#gid=0
|
* @param {string} sheetId - https://docs.google.com/spreadsheets/d/[[spreadsheetId]]/edit#gid=0
|
||||||
* @param {string} worksheet - the name of the worksheet containing ontime data
|
* @param {string} worksheet - the name of the worksheet containing ontime data
|
||||||
* @returns {Promise<false | {worksheetId: number, range: string}>} - false if not found | true if sheetId existes | id of worksheet and rage of worksheet
|
* @returns {Promise<{worksheetId: number, range: string}>} - id of worksheet and rage of worksheet
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
private async exist(
|
private async exist(sheetId: string, worksheet: string): Promise<{ worksheetId: number; range: string }> {
|
||||||
sheetId: string,
|
|
||||||
worksheet: string,
|
|
||||||
): Promise<false | true | { worksheetId: number; range: string }> {
|
|
||||||
const spreadsheets = await sheets({ version: 'v4', auth: sheet.client }).spreadsheets.get({
|
const spreadsheets = await sheets({ version: 'v4', auth: sheet.client }).spreadsheets.get({
|
||||||
spreadsheetId: sheetId,
|
spreadsheetId: sheetId,
|
||||||
});
|
});
|
||||||
@@ -132,11 +108,10 @@ class sheet {
|
|||||||
ourWorksheetData.properties.gridProperties.columnCount,
|
ourWorksheetData.properties.gridProperties.columnCount,
|
||||||
);
|
);
|
||||||
return { worksheetId: ourWorksheetData.properties.sheetId, range: `${worksheet}!A1:${endCell}` };
|
return { worksheetId: ourWorksheetData.properties.sheetId, range: `${worksheet}!A1:${endCell}` };
|
||||||
} else {
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
throw new Error('Uable to open spreadsheets');
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -144,16 +119,14 @@ class sheet {
|
|||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public async push() {
|
public async push() {
|
||||||
const { auth, id, worksheet } = await this.getSheetState();
|
const { id, worksheet } = DataProvider.getGoogleSheet();
|
||||||
if (!auth && !id && !worksheet) {
|
const { worksheetId, range } = await this.exist(id, worksheet);
|
||||||
throw new Error(`Sheet not authorized or incorrect ID or worksheet`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const rq = await sheets({ version: 'v4', auth: sheet.client }).spreadsheets.values.get({
|
const rq = await sheets({ version: 'v4', auth: sheet.client }).spreadsheets.values.get({
|
||||||
spreadsheetId: this.sheetId,
|
spreadsheetId: id,
|
||||||
valueRenderOption: 'FORMATTED_VALUE',
|
valueRenderOption: 'FORMATTED_VALUE',
|
||||||
majorDimension: 'ROWS',
|
majorDimension: 'ROWS',
|
||||||
range: this.range,
|
range: range,
|
||||||
});
|
});
|
||||||
if (rq.status === 200) {
|
if (rq.status === 200) {
|
||||||
const { rundownMetadata, projectMetadata } = parseExcel(rq.data.values);
|
const { rundownMetadata, projectMetadata } = parseExcel(rq.data.values);
|
||||||
@@ -171,13 +144,13 @@ class sheet {
|
|||||||
dimension: 'ROWS',
|
dimension: 'ROWS',
|
||||||
startIndex: titleRow + 1,
|
startIndex: titleRow + 1,
|
||||||
endIndex: titleRow + 2,
|
endIndex: titleRow + 2,
|
||||||
sheetId: this.worksheetId,
|
sheetId: worksheetId,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
//and delete the rest
|
//and delete the rest
|
||||||
updateRundown.push({
|
updateRundown.push({
|
||||||
deleteDimension: { range: { dimension: 'ROWS', startIndex: titleRow + 2, sheetId: this.worksheetId } },
|
deleteDimension: { range: { dimension: 'ROWS', startIndex: titleRow + 2, sheetId: worksheetId } },
|
||||||
});
|
});
|
||||||
// insert the lenght of the rundown
|
// insert the lenght of the rundown
|
||||||
updateRundown.push({
|
updateRundown.push({
|
||||||
@@ -187,24 +160,24 @@ class sheet {
|
|||||||
dimension: 'ROWS',
|
dimension: 'ROWS',
|
||||||
startIndex: titleRow + 1,
|
startIndex: titleRow + 1,
|
||||||
endIndex: titleRow + rundown.length,
|
endIndex: titleRow + rundown.length,
|
||||||
sheetId: this.worksheetId,
|
sheetId: worksheetId,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
//update the corresponding row with event data
|
//update the corresponding row with event data
|
||||||
rundown.forEach((entry, index) =>
|
rundown.forEach((entry, index) =>
|
||||||
updateRundown.push(cellRequenstFromEvent(entry, index, this.worksheetId, rundownMetadata)),
|
updateRundown.push(cellRequenstFromEvent(entry, index, worksheetId, rundownMetadata)),
|
||||||
);
|
);
|
||||||
|
|
||||||
//update project data
|
//update project data
|
||||||
updateRundown.push(cellRequenstFromProjectData(projectData, this.worksheetId, projectMetadata));
|
updateRundown.push(cellRequenstFromProjectData(projectData, worksheetId, projectMetadata));
|
||||||
|
|
||||||
const writeResponds = await sheets({ version: 'v4', auth: sheet.client }).spreadsheets.batchUpdate({
|
const writeResponds = await sheets({ version: 'v4', auth: sheet.client }).spreadsheets.batchUpdate({
|
||||||
spreadsheetId: this.sheetId,
|
spreadsheetId: id,
|
||||||
requestBody: {
|
requestBody: {
|
||||||
includeSpreadsheetInResponse: false,
|
includeSpreadsheetInResponse: false,
|
||||||
responseRanges: [this.range],
|
responseRanges: [range],
|
||||||
requests: updateRundown,
|
requests: updateRundown,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@@ -225,19 +198,18 @@ class sheet {
|
|||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public async pull(): Promise<Partial<ResponseOK>> {
|
public async pull(): Promise<Partial<ResponseOK>> {
|
||||||
const { auth, id, worksheet } = await this.getSheetState();
|
const { id, worksheet } = DataProvider.getGoogleSheet();
|
||||||
if (!auth && !id && !worksheet) {
|
const { range } = await this.exist(id, worksheet);
|
||||||
throw new Error(`Sheet not authorized or incorrect ID or worksheet`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const res: Partial<ResponseOK> = {};
|
const res: Partial<ResponseOK> = {};
|
||||||
|
|
||||||
const rq = await sheets({ version: 'v4', auth: sheet.client }).spreadsheets.values.get({
|
const rq = await sheets({ version: 'v4', auth: sheet.client }).spreadsheets.values.get({
|
||||||
spreadsheetId: this.sheetId,
|
spreadsheetId: id,
|
||||||
valueRenderOption: 'FORMATTED_VALUE',
|
valueRenderOption: 'FORMATTED_VALUE',
|
||||||
majorDimension: 'ROWS',
|
majorDimension: 'ROWS',
|
||||||
range: this.range,
|
range,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (rq.status === 200) {
|
if (rq.status === 200) {
|
||||||
res.data = {};
|
res.data = {};
|
||||||
const dataFromSheet = parseExcel(rq.data.values);
|
const dataFromSheet = parseExcel(rq.data.values);
|
||||||
|
|||||||
Reference in New Issue
Block a user