Skip to content

Instantly share code, notes, and snippets.

@adist98
Created April 14, 2021 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adist98/e5cbd516e6beef258588b49a110485e5 to your computer and use it in GitHub Desktop.
Save adist98/e5cbd516e6beef258588b49a110485e5 to your computer and use it in GitHub Desktop.
This code was written to create clickUp tasks automatically based off of entries in google form. The idea is to manage stuff in a structural manner and keep a record accordingly.
// coded and assembled by adist98
const fs = require('fs');
const readline = require('readline');
const {google} = require('googleapis');
const { fileURLToPath } = require('url');
// 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 = 'token.json';
// Load client secrets from a local file.
fs.readFile('credentials.json', (err, content) => {
if (err) return console.log('Error loading client secret file:', err);
// Authorize a client with credentials, then call the Google Sheets API.
authorize(JSON.parse(content), listMajors);
});
/**
* Create an OAuth2 client with the given credentials, and then execute the
* given callback function.
* @param {Object} credentials The authorization client credentials.
* @param {function} callback The callback to call with the authorized client.
*/
function authorize(credentials, callback) {
const {client_secret, client_id, redirect_uris} = credentials.installed;
const oAuth2Client = new google.auth.OAuth2(
client_id, client_secret, redirect_uris[0]);
// Check if we have previously stored a token.
fs.readFile(TOKEN_PATH, (err, token) => {
if (err) return getNewToken(oAuth2Client, callback);
oAuth2Client.setCredentials(JSON.parse(token));
callback(oAuth2Client);
});
}
/**
* Get and store new token after prompting for user authorization, and then
* execute the given callback with the authorized OAuth2 client.
* @param {google.auth.OAuth2} oAuth2Client The OAuth2 client to get token for.
* @param {getEventsCallback} callback The callback for the authorized client.
*/
function getNewToken(oAuth2Client, callback) {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES,
});
console.log('Authorize this app by visiting this url:', authUrl);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('4/1AY0e-g7Wn6mpYCKH6m28KHu6SJilA--jdnh1au7O2Ac_Maz-MeHFzKpPE3A', (code) => {
rl.close();
oAuth2Client.getToken(code, (err, token) => {
if (err) return console.error('Error while trying to retrieve access token', err);
oAuth2Client.setCredentials(token);
// Store the token to disk for later program executions
fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
if (err) return console.error(err);
console.log('Token stored to', TOKEN_PATH);
});
callback(oAuth2Client);
});
});
}
/**
* Prints the names and majors of students in a sample spreadsheet:
* @see https://docs.google.com/spreadsheets/d/1S5UpV6z1JfkI7oxMzd7fgbFUPFzHpkVstcLUULQ8ll4/edit
* @param {google.auth.OAuth2} auth The authenticated Google OAuth client.
*/
var temp2
if(temp){
temp2 = temp;
}
var temp = 0;
temp = temp2;
function listMajors(auth) {
const sheets = google.sheets({version: 'v4', auth});
sheets.spreadsheets.values.get({
spreadsheetId: '1S5UpV6z1JfkI7oxMzd7fgbFUPFzHpkVstcLUULQ8ll4',
range: 'A:B',
}, (err, res) => {
if (err) return console.log('The API returned an error: ' + err);
const rows = res.data.values;
let arr = [];
if (rows.length) {
console.log('Name, Major:');
rows.map((row) => {
console.log(`${row[0]}, ${row[1]}`);
// Putting values in an array
arr.push(`${row[0]}`);
});
} else {
console.log('No data found.');
}
//console.log(`${row[0][1]}`);
const fs = require('fs');
var temp = 0;
fs.readFile('rows.txt', 'utf8', function(err, data) {
// check if there is an entry in the google form
if(data == ''){
var x = rows.length.toString();
fs.writeFile('rows.txt', x, (err) => {
// throws an error, you could also catch it here
if (err) throw err;
console.log('rows saved!');
});
}else{
// go from temp to rows.length //
temp = Number(data);
if(temp != rows.length){
// for loop
var i;
console.log(temp);
for(i=temp; i<rows.length; i++){
//create task
var request = require('request');
request({
method: 'POST',
url: 'https://api.clickup.com/api/v2/list/7797925/task/',
headers: {
'Authorization': 'pk_3476872_BZVR7G77327XV7LPJVTL2NNB3EIF10Q2',
'Content-Type': 'application/json'
},
// ClickUp task details
body: `{ \"name\": \"${arr[i]}\" , \"description\": \"New Task Description\", \"assignees\": [ 183 ], \"tags\": [ \"tag name 1\" ], \"status\": \"Open\", \"priority\": 3, \"due_date\": 1508369194377, \"due_date_time\": false, \"time_estimate\": 8640000, \"start_date\": 1567780450202, \"start_date_time\": false, \"notify_all\": true, \"parent\": null, \"links_to\": null, \"check_required_custom_fields\": true, \"custom_fields\": [ { \"id\": \"0a52c486-5f05-403b-b4fd-c512ff05131c\", \"value\": 23 }, { \"id\": \"03efda77-c7a0-42d3-8afd-fd546353c2f5\", \"value\": \"Text field input\" } ]}`
}, function (error, response, body) {
console.log('Status:', response.statusCode);
console.log('Headers:', JSON.stringify(response.headers));
console.log('Response:', body);
});
}
// update temps val
fs.writeFile('rows.txt', rows.length.toString(), (err) => {
// throws an error, you could also catch it here
if (err) throw err;
//console.log('rows saved!');
});
temp = rows.length;
}
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment