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