-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.js
More file actions
77 lines (66 loc) · 2.08 KB
/
Copy pathCode.js
File metadata and controls
77 lines (66 loc) · 2.08 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
var sheetId = 'YOUR_SHEET_ID'; // Replace with your Google Sheet ID
var monitoredCell = 'C9'; // Replace with the cell you want to monitor
var emailRecipient = 'YOUR_EMAIL_ADDRESS'; // Replace with the recipient's email address
function checkCellValue() {
var sheet;
try {
sheet = SpreadsheetApp.openById(sheetId);
} catch (e) {
Logger.log('Error accessing spreadsheet: ' + e.toString());
return;
}
var cell;
try {
cell = sheet.getRange(monitoredCell);
} catch (e) {
Logger.log('Error accessing cell: ' + e.toString());
return;
}
var newValue;
try {
newValue = cell.getValue();
} catch (e) {
Logger.log('Error getting cell value: ' + e.toString());
return;
}
var lastValue = PropertiesService.getScriptProperties().getProperty('lastValue');
if (newValue != lastValue) {
sendEmailNotification(newValue, lastValue);
PropertiesService.getScriptProperties().setProperty('lastValue', newValue);
}
}
function sendEmailNotification(newValue, lastValue) {
var subject = 'Cell Value Changed';
var body = 'The value of cell ' + monitoredCell + ' has changed from ' + lastValue + ' to ' + newValue + '.';
try {
MailApp.sendEmail(emailRecipient, subject, body);
} catch (e) {
Logger.log('Error sending email: ' + e.toString());
}
}
function createTrigger() {
// Delete all existing triggers to avoid duplicates
var triggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < triggers.length; i++) {
ScriptApp.deleteTrigger(triggers[i]);
}
// Create a new time-based trigger to run checkCellValue every 5 minutes
try {
ScriptApp.newTrigger('checkCellValue')
.timeBased()
.everyMinutes(5) // Adjust the frequency as needed
.create();
} catch (e) {
Logger.log('Error creating trigger: ' + e.toString());
}
}
function testAccess() {
var sheet;
try {
var sheet = SpreadsheetApp.openById(sheetId);
Logger.log('Sheet Name: ' + sheet.getName());
Logger.log('Sheet URL: ' + sheet.getUrl());
} catch (e) {
Logger.log('Error accessing spreadsheet: ' + e.toString());
}
}