Fix gmail-label-cleanup: use GmailLabel methods instead of GmailApp

This commit is contained in:
2026-03-06 16:03:20 +00:00
parent 3c6e1588e7
commit 1bb41f5cc9
+24 -24
View File
@@ -42,8 +42,8 @@ function mergeLabels() {
var threads = fromLabel.getThreads(0, 500); var threads = fromLabel.getThreads(0, 500);
var total = 0; var total = 0;
while (threads.length > 0) { while (threads.length > 0) {
GmailApp.addLabelToThreads(threads, toLabel); toLabel.addToThreads(threads);
GmailApp.removeLabelFromThreads(threads, fromLabel); fromLabel.removeFromThreads(threads);
total += threads.length; total += threads.length;
Utilities.sleep(1000); Utilities.sleep(1000);
threads = fromLabel.getThreads(0, 500); threads = fromLabel.getThreads(0, 500);
@@ -72,9 +72,9 @@ function archiveCvlp() {
var threads = cvlpLabel.getThreads(0, 500); var threads = cvlpLabel.getThreads(0, 500);
var total = 0; var total = 0;
while (threads.length > 0) { while (threads.length > 0) {
GmailApp.addLabelToThreads(threads, newsletterLabel); newsletterLabel.addToThreads(threads);
GmailApp.removeLabelFromThreads(threads, cvlpLabel); cvlpLabel.removeFromThreads(threads);
GmailApp.moveThreadsToArchive(threads); threads.forEach(function(t) { t.moveToArchive(); });
total += threads.length; total += threads.length;
Utilities.sleep(1000); Utilities.sleep(1000);
threads = cvlpLabel.getThreads(0, 500); threads = cvlpLabel.getThreads(0, 500);
@@ -102,8 +102,8 @@ function mergePurchasesIntoReceipt() {
var threads = purchasesLabel.getThreads(0, 500); var threads = purchasesLabel.getThreads(0, 500);
var total = 0; var total = 0;
while (threads.length > 0) { while (threads.length > 0) {
GmailApp.addLabelToThreads(threads, receiptLabel); receiptLabel.addToThreads(threads);
GmailApp.removeLabelFromThreads(threads, purchasesLabel); purchasesLabel.removeFromThreads(threads);
total += threads.length; total += threads.length;
Utilities.sleep(1000); Utilities.sleep(1000);
threads = purchasesLabel.getThreads(0, 500); threads = purchasesLabel.getThreads(0, 500);
@@ -127,8 +127,8 @@ function restructureBusinessLabels() {
var threads = bizDonations.getThreads(0, 500); var threads = bizDonations.getThreads(0, 500);
var total = 0; var total = 0;
while (threads.length > 0) { while (threads.length > 0) {
GmailApp.addLabelToThreads(threads, donationsLabel); donationsLabel.addToThreads(threads);
GmailApp.removeLabelFromThreads(threads, bizDonations); bizDonations.removeFromThreads(threads);
total += threads.length; total += threads.length;
Utilities.sleep(1000); Utilities.sleep(1000);
threads = bizDonations.getThreads(0, 500); threads = bizDonations.getThreads(0, 500);
@@ -136,8 +136,7 @@ function restructureBusinessLabels() {
Logger.log('Moved ' + total + ' threads to top-level donations'); Logger.log('Moved ' + total + ' threads to top-level donations');
bizDonations.deleteLabel(); bizDonations.deleteLabel();
} else { } else {
// Try top-level donations label Logger.log('business/donations not found - skipping');
Logger.log('business/donations not found - checking for top-level donations label');
} }
// Move selling under business: rename selling -> business/selling // Move selling under business: rename selling -> business/selling
@@ -151,8 +150,8 @@ function restructureBusinessLabels() {
var threads = sellingLabel.getThreads(0, 500); var threads = sellingLabel.getThreads(0, 500);
var total = 0; var total = 0;
while (threads.length > 0) { while (threads.length > 0) {
GmailApp.addLabelToThreads(threads, bizSelling); bizSelling.addToThreads(threads);
GmailApp.removeLabelFromThreads(threads, sellingLabel); sellingLabel.removeFromThreads(threads);
total += threads.length; total += threads.length;
Utilities.sleep(1000); Utilities.sleep(1000);
threads = sellingLabel.getThreads(0, 500); threads = sellingLabel.getThreads(0, 500);
@@ -161,7 +160,7 @@ function restructureBusinessLabels() {
sellingLabel.deleteLabel(); sellingLabel.deleteLabel();
} }
// Delete contract label (threads already have business label, just remove contract) // Delete contract label (threads already have business label)
var contractLabel = GmailApp.getUserLabelByName('business/contract'); var contractLabel = GmailApp.getUserLabelByName('business/contract');
if (!contractLabel) { if (!contractLabel) {
contractLabel = GmailApp.getUserLabelByName('contract'); contractLabel = GmailApp.getUserLabelByName('contract');
@@ -171,7 +170,7 @@ function restructureBusinessLabels() {
var threads = contractLabel.getThreads(0, 500); var threads = contractLabel.getThreads(0, 500);
var total = 0; var total = 0;
while (threads.length > 0) { while (threads.length > 0) {
GmailApp.removeLabelFromThreads(threads, contractLabel); contractLabel.removeFromThreads(threads);
total += threads.length; total += threads.length;
Utilities.sleep(1000); Utilities.sleep(1000);
threads = contractLabel.getThreads(0, 500); threads = contractLabel.getThreads(0, 500);
@@ -202,15 +201,16 @@ function deleteUnusedLabels() {
Logger.log('Not found (already gone?): ' + name); Logger.log('Not found (already gone?): ' + name);
return; return;
} }
var count = label.getThreads(0, 1).length; var threads = label.getThreads(0, 500);
if (count > 0) { var total = 0;
Logger.log('WARNING: ' + name + ' still has threads - removing label from them first'); while (threads.length > 0) {
var threads = label.getThreads(0, 500); label.removeFromThreads(threads);
while (threads.length > 0) { total += threads.length;
GmailApp.removeLabelFromThreads(threads, label); Utilities.sleep(500);
Utilities.sleep(500); threads = label.getThreads(0, 500);
threads = label.getThreads(0, 500); }
} if (total > 0) {
Logger.log('Removed label from ' + total + ' threads: ' + name);
} }
label.deleteLabel(); label.deleteLabel();
Logger.log('Deleted: ' + name); Logger.log('Deleted: ' + name);