回頭來說明這個程式
可以分為兩個部分
1.將網路上的尼采名言整理放在Google試算表
2.在試算表新增App Script並連結到Line Notify
以下分別說明
1.將網路上的尼采名言整理放在Google試算表
Google關鍵字-尼采名言,就會有一大堆,雖然也不知道到底是不是尼采所言
總之挑選了一些比較有積極面向的語錄存放在試算表
2.在試算表新增App Script並連結到Line Notify
function runToLine()是啟動程式
在AppScript設置以時間為條件來進行觸發
因為不想半夜還收到訊息,所以設置在每天的7點到22點才進一步觸發主程式function NietzscheToLine()
function NietzscheToLine()是主程式
以試算表的資料筆數為範圍進行亂數取數,運用了 Math.floor(Math.random()*(max-min+1))+min 這樣的規則
取得介於1~筆數的隨機整數
為了避免前後次取道相同的數值,所以必須記錄本次取得的筆數,做為下一次數值的比較依據
將取得的數值對應試算表的資料欄位,取出儲存格內的資料
最後傳遞到sendMessage3()這個處理LINE Notify的程序
將訊息傳推播出去
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 | function runToLine(){
var time =new Date();
var runTime = time.getHours();
//Logger.log(time);
if(runTime >=7 && runTime <=22){ // 07:00 ~ 22:00
NietzscheToLine();
}else{
return;
}
}
function NietzscheToLine() {
// https://docs.google.com/spreadsheets/d/********************************************/
// sheet "Nietzsche"
var ss = SpreadsheetApp.openById("********************************************").getSheetByName("Nietzsche");
var Row = ss.getLastRow();
//Logger.log('Row- '+Row);
var g = ss.getRange("B2").getValue(); //取得在B2儲存上一次的序號
//Logger.log(g);
//Math.floor(Math.random()*(max-min+1))+min
var i = Math.floor(Math.random()*(Row-1+1))+1; // 本次序號
//Logger.log(i);
while( g == i){
i = Math.floor(Math.random()*(Row-1+1))+1;
}
var msg = "\n" + i + "-" +ss.getRange("A" + i).getValue();
//Logger.log(msg);
sendMessage3(msg.toString(), "446", "1990");
ss.getRange("B2").setValue(i) ; //寫出本次序號
}
function sendMessage3(message, stickerPackageId, stickerId){
var Token = "23rGWk1BMDeplI3G0Nue9e7dakMtLAwje0kUdGu4VrK";
var URL = "https://notify-api.line.me/api/notify";
var payload =
{
'message' : message,
//'imageThumbnail':'https://ih1.redbubble.net/image.1317550651.4451/flat,128x128,075,f-pad,128x128,f8f8f8.jpg',
//'imageFullsize':'https://ih1.redbubble.net/image.1317550651.4451/flat,128x128,075,f-pad,128x128,f8f8f8.jpg'
'stickerPackageId': stickerPackageId,
'stickerId': stickerId
};
var header =
{
'Content-Type':'application/x-www-form-urlencoded',
'Authorization' : 'Bearer ' + Token
}
var options =
{
'method' : 'post',
'payload' : payload,
'headers' : header
};
var response = UrlFetchApp.fetch(URL, options);
//Logger.log("response"+response);
}
|