-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy-sheet-oauth.js
More file actions
71 lines (61 loc) · 2.52 KB
/
Copy pathcopy-sheet-oauth.js
File metadata and controls
71 lines (61 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const { getOAuthDriveApi, getOAuthSheetsApi } = require('./oauth-setup');
const SOURCE_SHEET_ID = '1bRTR85CDHbTAsTG6sVi1jv6H-WjLyf-yvOerLCqeY2Q';
async function copyAndSetupSheet() {
console.log('Copying pizza-chef sheet for agar.pizza (using OAuth)...\n');
try {
const drive = await getOAuthDriveApi();
const sheets = await getOAuthSheetsApi();
// 1. Copy the spreadsheet
console.log('1. Copying spreadsheet...');
const copyResponse = await drive.files.copy({
fileId: SOURCE_SHEET_ID,
requestBody: {
name: 'agar pizza',
},
});
const newSheetId = copyResponse.data.id;
console.log(` ✓ Created new sheet: ${newSheetId}`);
// 2. Make it public (anyone with link can edit)
console.log('2. Making sheet public...');
await drive.permissions.create({
fileId: newSheetId,
requestBody: {
role: 'writer',
type: 'anyone',
},
});
console.log(' ✓ Sheet is now public (anyone with link can edit)');
// 3. Get sheet info to find the tasks sheet
console.log('3. Getting sheet structure...');
const sheetInfo = await sheets.spreadsheets.get({
spreadsheetId: newSheetId,
});
const sheetNames = sheetInfo.data.sheets.map(s => s.properties.title);
console.log(` Found sheets: ${sheetNames.join(', ')}`);
// 4. Clear tasks from all relevant sheets (keep headers)
console.log('4. Clearing tasks...');
for (const sheetName of sheetNames) {
try {
await sheets.spreadsheets.values.clear({
spreadsheetId: newSheetId,
range: `'${sheetName}'!A2:Z1000`,
});
console.log(` ✓ Cleared ${sheetName}`);
} catch (e) {
console.log(` - Skipped ${sheetName}: ${e.message}`);
}
}
// 5. Output the URL
const sheetUrl = `https://docs.google.com/spreadsheets/d/${newSheetId}/edit`;
console.log('\n✅ Done!\n');
console.log('═══════════════════════════════════════════════════════════');
console.log(`Sheet URL: ${sheetUrl}`);
console.log(`Sheet ID: ${newSheetId}`);
console.log('═══════════════════════════════════════════════════════════');
return { sheetId: newSheetId, sheetUrl };
} catch (error) {
console.error('❌ Error:', error.message);
throw error;
}
}
copyAndSetupSheet();