sheets
This commit is contained in:
		
							parent
							
								
									a2a10a9853
								
							
						
					
					
						commit
						981c2b9571
					
				
							
								
								
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@ -1,2 +1,4 @@
 | 
				
			|||||||
secrets.env
 | 
					secrets.env
 | 
				
			||||||
node_modules
 | 
					node_modules
 | 
				
			||||||
 | 
					token.json
 | 
				
			||||||
 | 
					credentials.json
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										52
									
								
								load.gs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								load.gs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,52 @@
 | 
				
			|||||||
 | 
					// this is straight up just copied from google appsscript
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function getFiles() {
 | 
				
			||||||
 | 
						const thisYear = new Date().getFullYear().toString()
 | 
				
			||||||
 | 
						const dregdayFolder = DriveApp.getFolderById(process.env.DREGDAY_FOLDER_ID)
 | 
				
			||||||
 | 
						const prepSheet = SpreadsheetApp.openById(process.env.DREGDAY_PREP_SHEET_ID)
 | 
				
			||||||
 | 
						const folder = dregdayFolder.getFoldersByName(thisYear).next()
 | 
				
			||||||
 | 
						const specials = folder.getFolders()
 | 
				
			||||||
 | 
						const dregs = folder.getFiles()
 | 
				
			||||||
 | 
						const entries = []
 | 
				
			||||||
 | 
					      
 | 
				
			||||||
 | 
						const makeEntry = (file) => {
 | 
				
			||||||
 | 
							return {
 | 
				
			||||||
 | 
								url: file.getUrl(),
 | 
				
			||||||
 | 
								direct: file.getDownloadUrl(),
 | 
				
			||||||
 | 
								name: file.getName(),
 | 
				
			||||||
 | 
								category: file.getParents().next().getName(),
 | 
				
			||||||
 | 
								mimeType: file.getMimeType(),
 | 
				
			||||||
 | 
								caption: ""
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						const isImage = mimeType => mimeType?.slice(0,6) === "image/"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						while (specials.hasNext()) {
 | 
				
			||||||
 | 
							const items = specials.next().getFiles()
 | 
				
			||||||
 | 
							while (items.hasNext()) {
 | 
				
			||||||
 | 
								const entry = makeEntry(items.next())
 | 
				
			||||||
 | 
								if (isImage(entry.mimeType)) entries.push(entry)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						while (dregs.hasNext()) {
 | 
				
			||||||
 | 
							const entry = makeEntry(dregs.next())
 | 
				
			||||||
 | 
							if (isImage(entry.mimeType)) entries.push(entry)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						if (!prepSheet.getSheetByName(thisYear)) prepSheet.insertSheet(thisYear)
 | 
				
			||||||
 | 
						else prepSheet.getSheetByName(thisYear).activate()
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						const active = prepSheet.getActiveSheet()
 | 
				
			||||||
 | 
						active.setFrozenRows(1)
 | 
				
			||||||
 | 
						active.setFrozenColumns(3)
 | 
				
			||||||
 | 
						active.getRange('A1:H1').setValues([["Status", "Category", "Name", "URL", "Machine Caption", "Image Description", "Post", "Notes"]])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for (let i = 0; i < entries.length; i++) {
 | 
				
			||||||
 | 
							const row = i+2
 | 
				
			||||||
 | 
						        const entry = entries[i]
 | 
				
			||||||
 | 
						        const category = entry.category.toString() === thisYear ? "General" : entry.category
 | 
				
			||||||
 | 
							active.getRange(row, 1, 1, 5).setValues([[false, entry.category, entry.name, entry.url, entry.caption]])
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										91
									
								
								ocr.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										91
									
								
								ocr.js
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,91 @@
 | 
				
			|||||||
 | 
					const fs = require('fs').promises;
 | 
				
			||||||
 | 
					const path = require('path');
 | 
				
			||||||
 | 
					const process = require('process');
 | 
				
			||||||
 | 
					const {authenticate} = require('@google-cloud/local-auth');
 | 
				
			||||||
 | 
					const {google} = require('googleapis');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// If modifying these scopes, delete token.json.
 | 
				
			||||||
 | 
					const SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly'];
 | 
				
			||||||
 | 
					// The file token.json stores the user's access and refresh tokens, and is
 | 
				
			||||||
 | 
					// created automatically when the authorization flow completes for the first
 | 
				
			||||||
 | 
					// time.
 | 
				
			||||||
 | 
					const TOKEN_PATH = path.join(process.cwd(), 'token.json');
 | 
				
			||||||
 | 
					const CREDENTIALS_PATH = path.join(process.cwd(), 'credentials.json');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Reads previously authorized credentials from the save file.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @return {Promise<OAuth2Client|null>}
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					async function loadSavedCredentialsIfExist() {
 | 
				
			||||||
 | 
					  try {
 | 
				
			||||||
 | 
					    const content = await fs.readFile(TOKEN_PATH);
 | 
				
			||||||
 | 
					    const credentials = JSON.parse(content);
 | 
				
			||||||
 | 
					    return google.auth.fromJSON(credentials);
 | 
				
			||||||
 | 
					  } catch (err) {
 | 
				
			||||||
 | 
					    return null;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Serializes credentials to a file compatible with GoogleAuth.fromJSON.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @param {OAuth2Client} client
 | 
				
			||||||
 | 
					 * @return {Promise<void>}
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					async function saveCredentials(client) {
 | 
				
			||||||
 | 
					  const content = await fs.readFile(CREDENTIALS_PATH);
 | 
				
			||||||
 | 
					  const keys = JSON.parse(content);
 | 
				
			||||||
 | 
					  const key = keys.installed || keys.web;
 | 
				
			||||||
 | 
					  const payload = JSON.stringify({
 | 
				
			||||||
 | 
					    type: 'authorized_user',
 | 
				
			||||||
 | 
					    client_id: key.client_id,
 | 
				
			||||||
 | 
					    client_secret: key.client_secret,
 | 
				
			||||||
 | 
					    refresh_token: client.credentials.refresh_token,
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					  await fs.writeFile(TOKEN_PATH, payload);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Load or request or authorization to call APIs.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					async function authorize() {
 | 
				
			||||||
 | 
					  let client = await loadSavedCredentialsIfExist();
 | 
				
			||||||
 | 
					  if (client) {
 | 
				
			||||||
 | 
					    return client;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  client = await authenticate({
 | 
				
			||||||
 | 
					    scopes: SCOPES,
 | 
				
			||||||
 | 
					    keyfilePath: CREDENTIALS_PATH,
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					  if (client.credentials) {
 | 
				
			||||||
 | 
					    await saveCredentials(client);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  return client;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Prints the names and majors of students in a sample spreadsheet:
 | 
				
			||||||
 | 
					 * @see https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
 | 
				
			||||||
 | 
					 * @param {google.auth.OAuth2} auth The authenticated Google OAuth client.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					async function listMajors(auth) {
 | 
				
			||||||
 | 
					  const sheets = google.sheets({version: 'v4', auth});
 | 
				
			||||||
 | 
					  const res = await sheets.spreadsheets.values.get({
 | 
				
			||||||
 | 
					    spreadsheetId: '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms',
 | 
				
			||||||
 | 
					    range: 'Class Data!A2:E',
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					  const rows = res.data.values;
 | 
				
			||||||
 | 
					  if (!rows || rows.length === 0) {
 | 
				
			||||||
 | 
					    console.log('No data found.');
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  console.log('Name, Major:');
 | 
				
			||||||
 | 
					  rows.forEach((row) => {
 | 
				
			||||||
 | 
					    // Print columns A and E, which correspond to indices 0 and 4.
 | 
				
			||||||
 | 
					    console.log(`${row[0]}, ${row[4]}`);
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					authorize().then(listMajors).catch(console.error);
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user