-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathost_managers.gs
More file actions
1234 lines (1040 loc) · 47.4 KB
/
ost_managers.gs
File metadata and controls
1234 lines (1040 loc) · 47.4 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function onOpen() {
var ui = SpreadsheetApp.getUi();
var menu = ui.createMenu('Custom')
menu.addItem('Go to Master\'s Sheet', 'goToMastersSheet');
menu.addItem('Go to Next Sheet', 'goToNextSheet')
menu.addItem('Go to Previous Sheet', 'goToPreviousSheet')
menu.addItem('Renumber Sheets', 'checkAndStartRenumbering')
menu.addSeparator();
menu.addItem('Input Media', 'openInputDialog')
menu.addItem('Sidebar', 'showSidebar'); // Existing menu item for the audio player
// Add the menu to the Google Sheets UI
menu.addToUi();
}
function showSidebar() {
// HTML content for the sidebar, including a simple interface to play music
var html = HtmlService.createHtmlOutputFromFile('Sidebar')
.setTitle('Sidebar ♬')
.setWidth(300);
SpreadsheetApp.getUi().showSidebar(html);
}
function goToMastersSheet() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var masterSheet = spreadsheet.getSheetByName('Master [OST 2022]'); // Replace with the name of your master sheet
if (masterSheet) {
spreadsheet.setActiveSheet(masterSheet);
} else {
SpreadsheetApp.getUi().alert('Master sheet not found.'); // Alert if the master sheet is not found
}
}
function goToNextSheet() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var currentSheet = spreadsheet.getActiveSheet();
var currentSheetName = currentSheet.getName();
var scriptProperties = PropertiesService.getScriptProperties();
var songData = JSON.parse(scriptProperties.getProperty("songData"));
for (var i = 0; i < songData.length; i++) {
if (songData[i].sheetName === currentSheetName) {
var nextSheetName = (i < songData.length - 1) ? songData[i + 1].sheetName : songData[0].sheetName;
spreadsheet.getSheetByName(nextSheetName).activate();
break;
}
}
}
function goToPreviousSheet() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var currentSheet = spreadsheet.getActiveSheet();
var currentSheetName = currentSheet.getName();
var scriptProperties = PropertiesService.getScriptProperties();
var songData = JSON.parse(scriptProperties.getProperty("songData"));
for (var i = 0; i < songData.length; i++) {
if (songData[i].sheetName === currentSheetName) {
var prevSheetName = (i > 0) ? songData[i - 1].sheetName : songData[songData.length - 1].sheetName;
spreadsheet.getSheetByName(prevSheetName).activate();
break;
}
}
}
function openInputDialog() {
var activeSheetName = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName();
// Assuming "Master [OST 2022]" is the name of the Master sheet
if (activeSheetName === "Master [OST 2022]") {
openDialogMasterSheet();
} else {
openDialogOtherSheets();
}
}
function openDialogMasterSheet() {
var html = HtmlService.createHtmlOutputFromFile('Dialog Master Sheet')
SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.showModalDialog(html, 'Input Media Link ♬');
}
function openDialogOtherSheets() {
var html = HtmlService.createHtmlOutputFromFile('Dialog Other Sheets')
SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.showModalDialog(html, 'Input Media Link ♬');
}
function matchSheetNamesToURLs() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const masterSheet = ss.getSheetByName("Master [OST 2022]"); // Adjust if your master sheet has a different name
const urlsRange = masterSheet.getRange("E2:E"); // Assumes URLs start from E2. Adjust if your range is different.
const urls = urlsRange.getValues();
let sheetNames = [];
// Loop through each URL and find the corresponding sheet name
urls.forEach(function(urlArray) {
const url = urlArray[0];
if (url) { // Check if URL is not empty
const sheetId = extractSheetIdFromUrl(url);
const sheet = findSheetById(ss, sheetId);
if (sheet) {
const sheetName = sheet.getName();
// Exclude the Master sheet
if (sheetName !== "Master [OST 2022]") {
sheetNames.push([sheetName]);
} else {
sheetNames.push([""]); // Keep the array aligned with URLs
}
} else {
sheetNames.push([""]); // In case no matching sheet is found
}
} else {
sheetNames.push([""]); // For empty cells in the URL column
}
});
// Write the matched sheet names to Column G, starting from G2
masterSheet.getRange(2, 7, sheetNames.length, 1).setValues(sheetNames); // Column G is index 7
}
function romanizeHangul(input) {
if (Array.isArray(input)) {
return input.map(romanizeHangul);
}
if (typeof input !== 'string') {
return "Invalid input. Please provide a string.";
}
var output = "";
for (var i = 0; i < input.length; i++) {
var unicode = input.charCodeAt(i);
if (unicode >= 44032 && unicode <= 55203) {
var syllableIndex = unicode - 44032;
var jong = syllableIndex % 28;
var jung = ((syllableIndex - jong) / 28) % 21;
var cho = (((syllableIndex - jong) / 28) - jung) / 21;
var choList = ["g", "gg", "n", "d", "dd", "r", "m", "b", "bb", "s", "ss", "", "j", "jj", "ch", "k", "t", "p", "h"];
var jungList = ["a", "ae", "ya", "yae", "eo", "e", "yeo", "ye", "o", "wa", "wae", "oe", "yo", "u", "weo", "we", "wi", "yu", "eu", "ui", "i"];
var jongList = ["", "g", "gg", "gs", "n", "nj", "nh", "d", "l", "lg", "lm", "lb", "ls", "lt", "lp", "lh", "m", "b", "bs", "s", "ss", "ng", "j", "ch", "k", "t", "p", "h"];
output += choList[cho] + jungList[jung] + jongList[jong];
} else {
output += input.charAt(i);
}
}
return output;
}
function onEdit(e) {
var editedSheet = e.range.getSheet();
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var masterSheet = spreadsheet.getSheetByName("Master [OST 2022]");
// Ensure the edit is not in the "Master [OST 2022]" sheet and is in column D
if (editedSheet.getName() !== "Master [OST 2022]" && e.range.getColumn() === 4) {
compactColumnD(sheet);
trimColumnD();
numberTracksAcrossSheetsBasedOnMaster(spreadsheet);}
// Direct synchronization: From "Master [OST 2022]" column H to sheet H2
if (editedSheet.getName() === "Master [OST 2022]" && e.range.getColumn() === 8) {
var urlCell = masterSheet.getRange(e.range.getRow(), 5); // URL is in column E
var sheetUrl = urlCell.getValue();
var modifiedColumnHValue = modifyUrlForPreview(e.range.getValue()); // Apply modification function
Logger.log("Direct Sync - URL: " + sheetUrl); // Logging the URL
var sheetId = extractSheetIdFromUrl(sheetUrl);
if (sheetId) {
var targetSheet = findSheetById(spreadsheet, sheetId);
if (targetSheet) {
targetSheet.getRange("H2").setValue(modifiedColumnHValue);
Logger.log("Direct Sync - Updated H2 in sheet ID: " + sheetId + " with value: " + modifiedColumnHValue);
} else {
Logger.log("Direct Sync - No target sheet found with ID: " + sheetId);
}
} else {
Logger.log("Direct Sync - No valid sheet ID extracted from URL: " + sheetUrl);
}
}
// Reverse synchronization: From any sheet H2 to "Master [OST 2022]" column H
else if (e.range.getA1Notation() === 'H2' && editedSheet.getName() !== "Master [OST 2022]") {
var allEntries = masterSheet.getRange('E2:H' + masterSheet.getLastRow()).getValues();
var editedSheetId = editedSheet.getSheetId().toString();
var found = false;
for (var i = 0; i < allEntries.length; i++) {
var rowSheetId = extractSheetIdFromUrl(allEntries[i][0]);
if (rowSheetId === editedSheetId) {
var modifiedColumnHValue = modifyUrlForPreview(e.range.getValue()); // Apply modification function
masterSheet.getRange(i + 2, 8).setValue(modifiedColumnHValue); // Update column H with modified value
found = true;
Logger.log("Reverse Sync - Updated Master [OST 2022] column H for Sheet ID: " + editedSheetId + " with value: " + modifiedColumnHValue);
break;
}
}
if (!found) {
Logger.log("Reverse Sync - No matching sheet found for Sheet ID: " + editedSheetId);
}
}
// New logic for Column I synchronization
// Direct synchronization: From "Master [OST 2022]" column I to other sheets' column I
if (editedSheet.getName() === "Master [OST 2022]" && e.range.getColumn() === 9) { // Column I is 9
var urlCell = masterSheet.getRange(e.range.getRow(), 5); // Assuming URL is in column E
var modifiedColumnIValue = convertToEmbedUrl(e.range.getValue()); // Apply conversion function
Logger.log("Direct Sync - URL: " + sheetUrl);
var sheetId = extractSheetIdFromUrl(urlCell.getValue()); // Reusing urlCell
if (sheetId) {
var targetSheet = findSheetById(spreadsheet, sheetId);
if (targetSheet) {
targetSheet.getRange("I2").setValue(modifiedColumnIValue); // Update Column I in target sheet
Logger.log("Direct Sync - Updated I2 in sheet ID: " + sheetId + " with value: " + modifiedColumnIValue);
} else {
Logger.log("Direct Sync - No target sheet found with ID: " + sheetId);
}
} else {
Logger.log("Direct Sync - No valid sheet ID extracted from URL: " + sheetUrl);
}
}
// Reverse synchronization: From any sheet's column I to "Master [OST 2022]" column I
else if (e.range.getA1Notation() === 'I2' && editedSheet.getName() !== "Master [OST 2022]") {
var allEntries = masterSheet.getRange('E2:I' + masterSheet.getLastRow()).getValues();
var editedSheetId = editedSheet.getSheetId().toString();
var found = false;
for (var i = 0; i < allEntries.length; i++) {
var rowSheetId = extractSheetIdFromUrl(allEntries[i][0]);
if (rowSheetId === editedSheetId) {
var modifiedColumnIValue = convertToEmbedUrl(e.range.getValue()); // Apply conversion function
masterSheet.getRange(i + 2, 9).setValue(modifiedColumnIValue); // Update Column I in master sheet
found = true;
Logger.log("Reverse Sync - Updated Master [OST 2022] column I for Sheet ID: " + editedSheetId + " with value: " + modifiedColumnIValue);
break;
}
}
if (!found) {
Logger.log("Reverse Sync - No matching sheet found for Sheet ID: " + editedSheetId);
}
}
//Youtube URL//
var editedCell = e.range;
// Check if the edited cell is in column I (column 9) and is not empty
if (editedCell.getColumn() == 9 && editedCell.getValue() != "") {
var newValue = convertToEmbedUrl(editedCell.getValue());
// Set the new value in the edited cell
editedCell.setValue(newValue);
}
// MP3 URL//
var editedCell = e.range
// Check if the edit is in column H and the cell is not empty
if (editedCell.getColumn() === 8 && editedCell.getValue() !== "") {
var originalValue = editedCell.getValue();
// Call modifyUrlForPreview with the cell's value
var newValue = modifyUrlForPreview(originalValue);
// Update the cell with the new value if it's different from the original value
if (newValue !== originalValue) {
editedCell.setValue(newValue);
}
}
// If the event object is not present, get the active spreadsheet
var sheet = e && e.source ? e.source.getSheetByName(e.range.getSheet().getName()) : SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
// Check if the sheet is not named "Master [OST 2022]" and column D"
if (editedSheet.getName() !== "Master [OST 2022]" && e.range.getColumn() === 4) {
var dataRange = sheet.getRange('D1:D' + sheet.getLastRow()); // Adjusted to get dynamic range up to the last row
var values = dataRange.getValues().flat(); // Flatten to a single-dimensional array for easier manipulation
var filteredValues = values.filter(value => value !== ""); // Remove empty strings
var numRows = filteredValues.length;
// Clear the original data range
dataRange.clearContent();
// Write back the filtered values to Column D, starting from the first cell
if (numRows > 0) {
sheet.getRange('D1:D' + numRows).setValues(filteredValues.map(value => [value]));
}
}
var range = e ? e.range : sheet.getActiveCell();
// Find all occurrences of the same hangul in column D
if (range.getColumn() == 6 && range.getValue() !== "") {
var translation = range.getValue();
var hangul = sheet.getRange(e.range.getRow(), 4).getValue().trim();
var duplicateRanges = sheet.getRange(1, 4, sheet.getLastRow(), 1)
.createTextFinder(hangul)
.findAll();
// Split the hangul into individual words
var hangulWords = hangul.split(" ");
// Update the translations in the duplicate rows in column F
duplicateRanges.forEach(function (duplicateRange) {
// Get the value in column D for the current row
var correspondingHangul = sheet.getRange(duplicateRange.getRow(), 4).getValue();
// Split the corresponding hangul into individual words
var correspondingHangulWords = correspondingHangul.split(" ");
// Check if the corresponding value in column D matches exactly
if (isExactMatch(hangulWords, correspondingHangulWords)) {
sheet.getRange(duplicateRange.getRow(), 6).setValue(translation); // Assuming column F is the 6th column
}
});
}
}
// Function to check if two arrays of words match exactly
function isExactMatch(words1, words2) {
if (words1.length !== words2.length) {
return false;
}
for (var i = 0; i < words1.length; i++) {
if (words1[i] !== words2[i]) {
return false;
}
}
return true;
}
function extractSheetIdFromUrl(url) {
var match = url.match(/#gid=(\d+)/);
return match ? match[1] : null;
}
function findSheetById(spreadsheet, sheetId) {
console.log("Attempting to find sheet with ID: ", sheetId);
var sheets = spreadsheet.getSheets();
for (var i = 0; i < sheets.length; i++) {
if (sheets[i].getSheetId().toString() === sheetId) {
return sheets[i];
}
}
return null;
}
// Function to retrieve filename from Google Drive link
function retrieveFilename() {
// Access the active spreadsheet
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getActiveSheet();
// Get the range of cells containing Google Drive links (assuming they are in column H)
var range = sheet.getRange('H2:H' + sheet.getLastRow());
// Get the values of the range (Google Drive links)
var links = range.getValues();
// Iterate through each link
links.forEach(function(link) {
if (link[0]) { // Check if the cell is not empty
var fileId = extractFileId(link[0]); // Extract file ID from the link
var filename = fileId ? retrieveFilename(fileId) : 'Invalid link'; // Retrieve filename using file ID
// Do something with the filename, such as logging it
Logger.log('Filename for ' + link[0] + ' is ' + filename);
}
});
}
// Function to extract file ID from Google Drive link
function extractFileId(link) {
var match = link.match(/\/d\/([^/]+)\//);
if (match && match[1]) {
return match[1];
} else {
return null;
}
}
// Function to retrieve filename using file ID
function retrieveFilename(fileId) {
var file = DriveApp.getFileById(fileId);
return file.getName();
}
function getMediaLinks() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var masterSheet = spreadsheet.getSheetByName("Master [OST 2022]");
var activeSheet = spreadsheet.getActiveSheet();
var sheetName = activeSheet.getName();
var links = [];
// Check if the active sheet is the Master sheet
if (sheetName === "Master [OST 2022]") {
var lastRow = masterSheet.getLastRow();
var range = masterSheet.getRange('B2:I' + lastRow);
var values = range.getValues();
// Fetch all song names and their corresponding media links
values.forEach(function(row) {
var songName = row[0]; // Column B: Song name
var audioLink = row[6]; // Column H: Audio link
var videoLink = row[7]; // Column I: Video link
if (audioLink || videoLink) {
var concatenatedLink = audioLink + '|' + videoLink;
links.push({ name: songName, link: concatenatedLink });
}
});
} else {
// For non-Master sheets, match sheet ID to URLs in the Master sheet to find the corresponding song title
var masterData = masterSheet.getRange('B2:E' + masterSheet.getLastRow()).getValues();
var activeSheetId = activeSheet.getSheetId().toString();
var songName = ""; // Initialize songName as an empty string
for (var i = 0; i < masterData.length; i++) {
var row = masterData[i];
var url = row[3]; // Column E: URL
var sheetIdFromUrl = extractSheetIdFromUrl(url);
if (sheetIdFromUrl === activeSheetId) {
songName = row[0]; // Column B: Song name
break; // Stop the loop once the matching song title is found
}
}
// Assuming audio and video links are in H2 and I2 of the active sheet
var audioLink = activeSheet.getRange('H2').getValue();
var videoLink = activeSheet.getRange('I2').getValue();
if (audioLink || videoLink) {
var concatenatedLink = audioLink + '|' + videoLink;
links.push({ name: songName, link: concatenatedLink });
}
}
return links;
}
// Function to convert a YouTube URL to the embed format
function convertToEmbedUrl(url) {
// If the URL is in the format https://www.youtube.com/watch?v=o1I87SHTh9c
if (url.includes('youtube.com/watch?v=')) {
return url.replace('youtube.com/watch?v=', 'youtube.com/embed/');
}
// If the URL is in the format https://youtu.be/o1I87SHTh9c
else if (url.includes('youtu.be/')) {
return url.replace('youtu.be/', 'youtube.com/embed/');
}
// If the URL format is unrecognized, return the original URL
else {
return url;
}
}
// Your existing openAudioPlayer function
const openAudioPlayer = () => {
const activeSheet = SpreadsheetApp.getActiveSheet();
const cell = activeSheet.getActiveCell().getValue();
var title = activeSheet.getName(); // Use sheet name as the title
const html = `<iframe src="${cell}" width="400" height="80" frameborder="0" scrolling="no"></iframe>`;
const dialog = HtmlService.createHtmlOutput(html).setTitle('Play').setWidth(420).setHeight(100);
SpreadsheetApp.getUi().showModelessDialog(dialog, `Play Audio: ${title}`);
};
const openYouTubeVideo = () => {
const activeSheet = SpreadsheetApp.getActiveSheet();
const cell = activeSheet.getActiveCell().getValue();
var title = activeSheet.getName(); // Use sheet name as the title
// Extract the VIDEO_ID from the YouTube URL
const videoIdMatch = cell.match(/(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/);
if (videoIdMatch && videoIdMatch[1]) {
const videoId = videoIdMatch[1];
const embedUrl = `https://www.youtube.com/embed/${videoId}`;
const html = `<iframe src="${embedUrl}" width="560" height="315" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>`;
const dialog = HtmlService.createHtmlOutput(html).setTitle('Watch Video').setWidth(580).setHeight(360);
SpreadsheetApp.getUi().showModelessDialog(dialog, `Watch Video: ${title}`);
} else {
SpreadsheetApp.getUi().alert('Invalid YouTube URL');
}
};
function getSheetData() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Master [OST 2022]");
var dataRange = sheet.getDataRange();
var data = dataRange.getValues();
var sheetData = [];
for (var i = 1; i < data.length; i++) { // Start at 1 to skip header row
var row = data[i];
sheetData.push({
songTrack: row[1], // Assuming song track is in the second column (B)
row: i + 1, // +1 because array is 0-indexed but sheets are 1-indexed
columnHValue: row[7], // Assuming column H values are in the 8th array position
columnIValue: row[8] // Assuming column I values are in the 9th array position
});
}
return sheetData;
}
function submitInfoForMaster(songTrack, columnHValue, columnIValue) {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName("Master [OST 2022]");
var dataRange = sheet.getDataRange();
var values = dataRange.getValues();
var modifiedColumnHValue = modifyUrlForPreview(columnHValue);
var modifiedColumnIValue = convertToEmbedUrl(columnIValue);
// Update the master sheet
for (var i = 0; i < values.length; i++) {
if (values[i][1] == songTrack) {
sheet.getRange(i + 1, 8).setValue(modifiedColumnHValue);
sheet.getRange(i + 1, 9).setValue(modifiedColumnIValue);
break; // Exit after updating
}
}
// Synchronize other sheets based on URLs in Column E
values.forEach(function(row) {
var url = row[4]; // Column E contains the URLs
if (url) {
var sheetId = extractSheetIdFromUrl(url);
var targetSheet = findSheetById(spreadsheet, sheetId);
if (targetSheet) {
targetSheet.getRange("H2").setValue(modifiedColumnHValue); // Update Column H in target sheet
targetSheet.getRange("I2").setValue(modifiedColumnIValue); // Update Column I in target sheet
}
}
});
}
function submitInfoForOtherSheets(columnHValue, columnIValue) {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var currentSheet = spreadsheet.getActiveSheet();
var masterSheet = spreadsheet.getSheetByName("Master [OST 2022]");
var sheetUrl = spreadsheet.getUrl() + "#gid=" + currentSheet.getSheetId();
var modifiedColumnHValue = modifyUrlForPreview(columnHValue);
var modifiedColumnIValue = convertToEmbedUrl(columnIValue);
currentSheet.getRange('H2').setValue(modifiedColumnHValue);
currentSheet.getRange('I2').setValue(modifiedColumnIValue);
var dataRange = masterSheet.getDataRange();
var values = dataRange.getValues();
var found = false;
for (var i = 1; i < values.length; i++) {
var rowUrl = values[i][4]; // Assuming URLs are in column E
if (rowUrl === sheetUrl) {
masterSheet.getRange(i + 1, 8).setValue(modifiedColumnHValue); // Column H for audio link
masterSheet.getRange(i + 1, 9).setValue(modifiedColumnIValue); // Column I for video link
found = true;
break;
}
}
if (!found) {
Logger.log('Sheet URL not found in master sheet');
}
}
function modifyUrlForPreview(url) {
// Check if the URL is empty or only contains whitespace, or does not include drive
if (!url || url.trim() === '' || !url.includes('drive')) {
return url; // Return the original URL without modification
}
// Normalize the URL by ensuring there's no trailing slash, which could interfere with replacements
url = url.replace(/\/+$/, '');
// Replace '/view?usp=sharing' or '/view?usp=drive_link' with '/preview', regardless of position
url = url.replace(/\/view\?usp=sharing/, '/preview');
url = url.replace(/\/view\?usp=drive_link/, '/preview');
// Check if the URL ends with '/view' or '/view/' and replace it with '/preview'
if (url.endsWith('/view/') || url.endsWith('/view')) {
url = url.replace(/\/view\/?$/, '/preview');
}
// For URLs with 'open?id=', replace with 'file/d/' and append '/preview', avoiding double slashes
else if (url.includes('open?id=')) {
url = url.replace('open?id=', 'file/d/');
// Append '/preview' if not already present and the URL doesn't end with '/', ensuring no double slashes
if (!url.endsWith('/preview') && !url.endsWith('/')) {
url += '/preview';
}
}
// Append '/preview' if none of the above conditions applied and it's not already there
else if (!url.endsWith('/preview')) {
url += '/preview';
}
return url;
}
function getTitleTrack() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Master [OST 2022]");
var range = sheet.getRange("B2:B" + sheet.getLastRow());
var values = range.getValues();
var tracks = []; // Use a more descriptive variable name like 'tracks' instead of 'links'
values.forEach(function(row) {
// Check if row value does not contain the symbol "】"
if (row[0] && !row[0].includes("】")) {
tracks.push(row[0]);
}
});
return tracks;
}
function fetchDramaNameForSong(columnBValue) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Master [OST 2022]");
var rows = sheet.getDataRange().getValues();
var dramaName = "";
for (var i = 0; i < rows.length; i++) {
var currentBValue = rows[i][1].toString().trim();
// If the current row's B column matches the selected song title
if (currentBValue === columnBValue.trim()) {
// Loop backwards to find the nearest preceding drama name, indicated by "】"
for (var j = i; j >= 0; j--) {
if (rows[j][1].includes("】")) {
dramaName = rows[j][1];
break; // Exit the loop once the drama name is found
}
}
break; // Exit the loop once the matching song and its drama name are found
}
}
return dramaName;
}
function getRowDataByColumnBValue(columnBValue) {
try {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Master [OST 2022]");
var rows = sheet.getDataRange().getValues();
var result = { rowData: null };
for (var i = 0; i < rows.length; i++) {
var currentBValue = rows[i][1].toString().trim();
if (currentBValue === columnBValue.trim()) {
result.rowData = {
columnC: rows[i][2],
columnD: rows[i][3],
columnE: rows[i][4],
columnF: rows[i][5],
columnH: rows[i][7],
columnI: rows[i][8]
};
break; // Exit the loop once the song details are found
}
}
Logger.log("Song details (excluding drama name): " + JSON.stringify(result));
return result;
} catch (e) {
Logger.log("Error: " + e.toString());
return null;
}
}
function getDialogRowData(songTrack) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Master [OST 2022]");
var data = sheet.getRange("B2:I" + sheet.getLastRow()).getValues(); // Assuming H and I contain the links
for (var i = 0; i < data.length; i++) {
if (data[i][0] === songTrack) { // Column B matches songTrack
return {audioLink: data[i][6], videoLink: data[i][7]}; // Columns H and I
}
}
return {audioLink: "", videoLink: ""}; // Return empty strings if not found
}
// This function fetches the list of valid options for Column D from the data validation rules.
// Adjust the range as necessary to match where your data validation is set up.
function getColumnDOptions() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Master [OST 2022]");
var range = sheet.getRange("D2:D"); // Adjust this range as needed
var rules = range.getDataValidations();
var options = [];
for (var i = 0; i < rules.length; i++) {
var rule = rules[i][0]; // Assuming single-column range
if (rule != null) {
var criteria = rule.getCriteriaType();
var args = rule.getCriteriaValues();
if (criteria == SpreadsheetApp.DataValidationCriteria.VALUE_IN_LIST) {
options = args[0];
break; // Assuming all cells in the column have the same validation rule
}
}
}
return options;
}
function updateMasterSheetAndSync(columnBValue, columnDValue, columnEValue, columnFValue, columnHValue, columnIValue) {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName("Master [OST 2022]");
var dataRange = sheet.getDataRange();
var values = dataRange.getValues();
// Fix variable names here: columnHValue and columnIValue instead of ColumnHValue and ColumnIValue
var modifiedColumnHValue = modifyUrlForPreview(columnHValue);
var modifiedColumnIValue = convertToEmbedUrl(columnIValue);
// Update the master sheet
for (var i = 0; i < values.length; i++) {
if (values[i][1] == columnBValue) {
sheet.getRange(i + 1, 4).setValue(columnDValue);
sheet.getRange(i + 1, 5).setValue(columnEValue);
sheet.getRange(i + 1, 6).setValue(columnFValue);
sheet.getRange(i + 1, 8).setValue(modifiedColumnHValue);
sheet.getRange(i + 1, 9).setValue(modifiedColumnIValue);
break; // Exit after updating
}
}
// Synchronize other sheets based on URLs in Column E
values.forEach(function(row) {
var url = row[4]; // Column E contains the URLs
if (url) {
var sheetId = extractSheetIdFromUrl(url);
var targetSheet = findSheetById(spreadsheet, sheetId);
if (targetSheet) {
// Find the row in target sheet to update. This might require custom logic.
targetSheet.getRange("H2").setValue(modifiedColumnHValue); // Update Column H in target sheet
targetSheet.getRange("I2").setValue(modifiedColumnIValue); // Update Column I in target sheet
}
}
});
}
function getSidebarPlaceholder() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var activeSheet = spreadsheet.getActiveSheet();
var masterSheet = spreadsheet.getSheetByName("Master [OST 2022]");
var isMasterSheet = activeSheet.getName() === "Master [OST 2022]";
var selectedSong = isMasterSheet ? null : determineSelectedSongForSheet();
var placeholder = isMasterSheet ? "Pick a song bruh" : (selectedSong || "Select a song");
var data = getTitleTrack();
if (!isMasterSheet) {
var activeSheetUrl = activeSheet.getFormUrl(); // Assuming there's a way to get the unique URL or ID of the active sheet
var urlsRange = masterSheet.getRange("E2:E" + masterSheet.getLastRow());
var urls = urlsRange.getValues();
for (var i = 0; i < urls.length; i++) {
if (urls[i][0] === activeSheetUrl) {
selectedSong = data[i];
break;
}
}
}
// Logging to help debug the behavior
Logger.log("Sheet Name: " + activeSheet.getName());
Logger.log("Is Master Sheet: " + isMasterSheet);
Logger.log("Selected Song: " + selectedSong);
Logger.log("Placeholder: " + placeholder);
return {
data: data, // The data for the dropdown options
placeholder: placeholder,
selectedSong: selectedSong, // The song that should be selected by default
isMasterSheet: isMasterSheet // Boolean indicating if the current sheet is the Master sheet
};
}
function determineSelectedSongForSheet() {
var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var isMasterSheet = activeSheet.getName() === "Master [OST 2022]";
if (isMasterSheet) {
// Possibly use getMasterSheetData() for specific operations within the Master sheet
} else {
var activeSheetId = activeSheet.getSheetId().toString();
var songSheetData = getSongSheetData();
var selectedSong = songSheetData.find(item => item.sheetId === activeSheetId)?.songName;
// Log for debugging
Logger.log("Selected Song for sheet '" + activeSheet.getName() + "': " + selectedSong);
return selectedSong;
}
}
function isMasterSheet() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var activeSheetName = spreadsheet.getActiveSheet().getName();
return activeSheetName === "Master [OST 2022]";
}
function findSheetByIdNew(sheetId) {
// Directly access the active spreadsheet without passing it as an argument
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheets = spreadsheet.getSheets();
// Convert sheetId to a number if it's passed as a string
var numericSheetId = parseInt(sheetId, 10);
for (var i = 0; i < sheets.length; i++) {
if (sheets[i].getSheetId() === numericSheetId) {
return sheets[i];
}
}
return null; // Return null if no matching sheet is found
}
function getSongSheetData() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var masterSheet = spreadsheet.getSheetByName("Master [OST 2022]");
var data = [];
var columnEValues = masterSheet.getRange('E2:E' + masterSheet.getLastRow()).getValues(); // URLs
var columnBValues = masterSheet.getRange('B2:B' + masterSheet.getLastRow()).getValues(); // Song names
columnEValues.forEach(function(eValue, i) {
if (eValue[0] !== '') {
var sheetId = extractSheetIdFromUrl(eValue[0]);
if (sheetId) {
data.push({ songName: columnBValues[i][0], sheetUrl: eValue[0], sheetId: sheetId }); // Include song name, URL, and sheetId
}
}
});
return data;
}
function matchingUrlforNumbering() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var masterSheet = spreadsheet.getSheetByName("Master [OST 2022]");
var columnBRange = masterSheet.getRange('B2:B' + masterSheet.getLastRow());
var columnERange = masterSheet.getRange('E2:E' + masterSheet.getLastRow());
var columnGRange = masterSheet.getRange('G2:G' + masterSheet.getLastRow());
var columnBValues = columnBRange.getValues();
var columnEValues = columnERange.getValues();
var columnGValues = columnGRange.getValues(); // Retrieve sheet names directly from column G
var songData = [];
var sequenceNumber = 0; // Start with 0, increment before assignment to start from 1
columnEValues.forEach(function(eValue, index) {
var url = eValue[0]; // Get the URL
var title = columnBValues[index][0]; // Get the title
var sheetName = columnGValues[index][0]; // Directly use the sheet name from column G
var isDramaTitle = title.toLowerCase().includes("】"); // Check if title contains "】"
if (url && !isDramaTitle) { // If there's a URL and it's not a drama title, proceed
sequenceNumber++; // Increment sequence number for each valid URL entry
var sheetId = extractSheetIdFromUrl(url); // Extract the sheet ID from the URL
// Push entry with sequence number, song name from column B, and sheet ID, including the directly retrieved sheet name
songData.push({
sequenceNumber: sequenceNumber,
sheetName: sheetName,
songName: title,
sheetId: sheetId,
});
}
});
// Store songData in Script Properties
var scriptProperties = PropertiesService.getScriptProperties();
scriptProperties.setProperty("songData", JSON.stringify(songData));
Logger.log(songData);
return songData;
}
// Function to store songData in Script Properties
function storeSongData() {
var songData = matchingUrlforNumbering();
var scriptProperties = PropertiesService.getScriptProperties();
scriptProperties.setProperty("songData", JSON.stringify(songData));
}
// Function to retrieve songData from Script Properties
function retrieveSongData() {
var scriptProperties = PropertiesService.getScriptProperties();
var songDataString = scriptProperties.getProperty("songData");
if (songDataString) {
return JSON.parse(songDataString);
}
return []; // Return an empty array if no data is found
}
function updateNumberingInfo(sheetId, firstNumber, lastNumber) {
if (!sheetId) {
console.error("No sheetId provided to updateNumberingInfo function.");
return;
}
// Retrieve the stored songData from script properties
var scriptProperties = PropertiesService.getScriptProperties();
var songDataJson = scriptProperties.getProperty("songData");
var songData = songDataJson ? JSON.parse(songDataJson) : [];
// Find the sheet information by sheetId
var sheetInfo = songData.find(item => item.sheetId == sheetId);
if (!sheetInfo) {
console.error("Sheet name not found for sheetId: " + sheetId);
return;
}
// Use the found sheet name and sheetId to update the numbering info
var numberingInfoJson = scriptProperties.getProperty("numberingInfo");
var numberingInfo = numberingInfoJson ? JSON.parse(numberingInfoJson) : {};
numberingInfo[sheetId] = {
sheetName: sheetInfo.sheetName, // Use the sheet name from the found sheet info
firstNumber: firstNumber,
lastNumber: lastNumber
};
// Store the updated numbering info back into script properties
scriptProperties.setProperty("numberingInfo", JSON.stringify(numberingInfo));
console.log(`Numbering info updated for ${sheetInfo.sheetName} (Sheet ID: ${sheetId})`);
}
function getStoredNumberingInfo() {
var scriptProperties = PropertiesService.getScriptProperties();
var numberingInfoString = scriptProperties.getProperty('numberingInfo');
var numberingInfo = numberingInfoString ? JSON.parse(numberingInfoString) : {};
return numberingInfo;
}
function numberTracksAcrossSheetsBasedOnMaster() {
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
const activeSheet = spreadsheet.getActiveSheet();
const activeSheetName = activeSheet.getName();
let numberingInfo = getStoredNumberingInfo();
const songData = retrieveSongData(); // Assuming this retrieves your song data correctly
const currentSheetData = songData.find(item => item.sheetName === activeSheetName);
if (!currentSheetData) {
console.log("Active sheet is not part of the sequence.");
return;
}
const sheetId = currentSheetData.sheetId;
let startingNumber = findStartingNumberFromContent(activeSheet, sheetId, songData);
const lastRow = activeSheet.getLastRow();
const columnDValues = activeSheet.getRange('D2:D' + lastRow).getValues();
let numberingValues = [];
columnDValues.forEach((cell, index) => {
if (cell[0] !== '') {
numberingValues.push([startingNumber++]);
} else {
numberingValues.push(['']); // Keep empty cells as is
}
});
if (numberingValues.length > 0) {
activeSheet.getRange(2, 3, numberingValues.length, 1).setValues(numberingValues);
}
// Assuming updateNumberingInfo correctly updates the numbering information
updateNumberingInfo(sheetId, startingNumber - numberingValues.length, startingNumber - 1);
// Update the stored numbering info to reflect this change
PropertiesService.getScriptProperties().setProperty('numberingInfo', JSON.stringify(numberingInfo));
console.log("Last number used:", startingNumber - 1);
}
function findStartingNumberFromContent(sheet, sheetId, songData) {
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// Determine the current sheet's index in the sequence
const currentIndex = songData.findIndex(data => data.sheetId === sheetId);
// For the first sheet in the sequence, start numbering from 1
if (currentIndex === 0) {
return 1;
}
// For subsequent sheets, find the last number used in the previous sheet
if (currentIndex > 0) {
const previousSheet = spreadsheet.getSheetByName(songData[currentIndex - 1].sheetName);
const lastRowOfPreviousSheet = findLastRowWithDataInColumnD(previousSheet);
const lastNumberCell = previousSheet.getRange('C' + lastRowOfPreviousSheet).getValue();
// Ensure the last number from the previous sheet is a number before returning the next number
if (!isNaN(lastNumberCell) && lastNumberCell) {
return parseInt(lastNumberCell) + 1;
}
}
// Fallback if unable to determine from content, though in practice this should not happen
Logger.log('Unable to determine starting number from content for sheet: ' + sheet.getName());
return 1; // Default to 1 or consider a different fallback mechanism
}
function findStartingNumberForSheet(sheetId) {
const numberingInfo = getStoredNumberingInfo();