-
Notifications
You must be signed in to change notification settings - Fork 0
/
BatchUrlDesc.js
137 lines (124 loc) · 4.48 KB
/
BatchUrlDesc.js
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
/**
* Initializes the batch processing and sets up a trigger to continue processing.
* @param {number} startRowUrlDesc The starting row number for processing.
* @param {number} endRowUrlDesc The ending row number for processing.
* @param {string} inColUrlDesc The column letter where the website url is.
* @param {string} outColUrlDesc The column letter where the results should be written.
*/
function startUrlDescProcessing(
startRowUrlDesc = 2,
endRowUrlDesc = 100,
inColUrlDesc = 'N',
outColUrlDesc = 'AH'
) {
deleteExistingTriggers('continueUrlDescProcessing');
const batchSize = 30; // Adjust this number based on the typical processing time per batch to stay under the 6-minute script runtime limit
let currentendRowUrlDesc = startRowUrlDesc + batchSize - 1;
if (currentendRowUrlDesc > endRowUrlDesc)
currentendRowUrlDesc = endRowUrlDesc;
// Store settings in script properties
const properties = PropertiesService.getScriptProperties();
properties.setProperties({
inColUrlDesc: inColUrlDesc,
outColUrlDesc: outColUrlDesc,
endRowUrlDesc: endRowUrlDesc.toString(),
lastRowProcessedUrlDesc: currentendRowUrlDesc.toString(),
});
// Start the first batch
processUrlDescBatches(
startRowUrlDesc,
currentendRowUrlDesc,
inColUrlDesc,
outColUrlDesc
);
if (currentendRowUrlDesc < endRowUrlDesc) {
ScriptApp.newTrigger('continueUrlDescProcessing')
.timeBased()
.after(10000) // Wait 10 seconds before continuing
.create();
}
}
/**
* Processes batches of prompts and writes the responses to a specified output column.
* @param {number} startRowUrlDesc The starting row number for processing.
* @param {number} endRowUrlDesc The ending row number for processing.
* @param {string} inColUrlDesc The column letter where the website url is.
* @param {string} outColUrlDesc The column letter where the results should be written.
*/
function processUrlDescBatches(
startRowUrlDesc,
endRowUrlDesc,
inColUrlDesc,
outColUrlDesc
) {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const promptsRange = `${inColUrlDesc}${startRowUrlDesc}:${inColUrlDesc}${endRowUrlDesc}`;
const prompts = sheet.getRange(promptsRange).getValues();
let results = [];
prompts.forEach((row, index) => {
if (row[0] !== '') {
try {
let response = UrlDescription(row[0]);
results.push([response]);
} catch (e) {
console.error('Error processing prompt: ' + row[0], e);
results.push(['Error: ' + e.toString()]);
}
} else {
results.push(['']);
}
});
const outputRange = `${outColUrlDesc}${startRowUrlDesc}:${outColUrlDesc}${
startRowUrlDesc + results.length - 1
}`;
sheet.getRange(outputRange).setValues(results);
}
/**
* Continues processing the next batch after a delay.
*/
function continueUrlDescProcessing() {
const properties = PropertiesService.getScriptProperties();
const inColUrlDesc = properties.getProperty('inColUrlDesc');
const outColUrlDesc = properties.getProperty('outColUrlDesc');
const endRowUrlDesc = parseInt(properties.getProperty('endRowUrlDesc'));
let lastRow = parseInt(properties.getProperty('lastRowProcessedUrlDesc'));
const batchSize = 30;
const startRowUrlDesc = lastRow + 1;
let nextendRowUrlDesc = startRowUrlDesc + batchSize - 1;
if (nextendRowUrlDesc > endRowUrlDesc) nextendRowUrlDesc = endRowUrlDesc;
// Process the next batch
processUrlDescBatches(
startRowUrlDesc,
nextendRowUrlDesc,
inColUrlDesc,
outColUrlDesc
);
// Update the last processed row
properties.setProperty(
'lastRowProcessedUrlDesc',
nextendRowUrlDesc.toString()
);
if (nextendRowUrlDesc < endRowUrlDesc) {
deleteExistingTriggers('continueUrlDescProcessing');
ScriptApp.newTrigger('continueUrlDescProcessing')
.timeBased()
.after(10000) // Wait 10 seconds before continuing again
.create();
} else {
// All batches are processed, delete the trigger
deleteExistingTriggers('continueUrlDescProcessing');
console.log('All rows have been processed.');
}
}
/**
* Deletes all triggers associated with a given function name.
* @param {string} functionName The name of the function whose triggers need to be deleted.
*/
function deleteExistingTriggers(functionName) {
var allTriggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < allTriggers.length; i++) {
if (allTriggers[i].getHandlerFunction() === functionName) {
ScriptApp.deleteTrigger(allTriggers[i]);
}
}
}