希望能夠將第二段程式碼功能,融入第一段程式碼,讓資料查詢時,不會因為資料過多而產生,無法正常回傳訊息問題,懇請高手們協助幫忙。
第一段程式碼
//取得LINE官方帳號傳來的資料
function doPost(e) {
var contents = JSON.parse(e.postData.contents);
var replyToken = contents.events[0].replyToken;
var userId = contents.events[0].source.userId;
var message = contents.events[0].message.text;
// 取得所有資料
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('試算表');
var data = sheet.getDataRange().getValues();
// 依照關鍵字篩選資料
var result = data.filter(function(row) {
return row.some(function(cell) {
return cell.toString().toLowerCase().includes(message);
});
});
// 判斷是否有查詢到資料
if (result.length > 0) {
var messages = [];
// 拼接訊息內容
for (var i = 0; i < result.length; i++) {
var messageObj = {
a: result[i][0], // A 欄位
b: result[i][1], // B 欄位
c: result[i][2], // C 欄位
url: result[i][3] // D 欄位
};
messages.push(messageObj);
}
// 回覆訊息
replyMessages(replyToken, messages);
} else {
replyMessage(replyToken, '查無資料');
}
}
// 回覆單一訊息給LINE官方帳號
function replyMessage(replyToken, message) {
var url = 'https://api.line.me/v2/bot/message/reply';
var headers = {
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': 'Bearer ' + accessToken,
};
var postData = {
'replyToken': replyToken,
'messages': [{
'type': 'text',
'text': message
}]
};
var options = {
'method': 'post',
'headers': headers,
'payload': JSON.stringify(postData),
};
UrlFetchApp.fetch(url, options);
}
// 回覆多個訊息給LINE官方帳號
function replyMessages(replyToken, messages) {
var url = 'https://api.line.me/v2/bot/message/reply';
var headers = {
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': 'Bearer ' + accessToken,
};
// 產生 Carousel 的內容
var contents = [];
for (var i = 0; i < messages.length; i++) {
contents.push({
"type": "bubble",
"hero": {
"type": "image",
"size": "full",
"aspectRatio": "20:13",
"aspectMode": "cover",
"url": messages[i].url
},
"body": {
"type": "box",
"layout": "vertical",
"spacing": "sm",
"contents": [
{
"type": "text",
"text":messages[i].b,
"wrap": true,
"weight": "bold",
"size": "xl",
"color": "#000000"
},
{
"type": "text",
"text": messages[i].b,
"size": "xl",
"weight": "bold"
},
{
"type": "text",
"text": messages[i].c,
"size": "xl",
"weight": "bold"
}
]
}
});
}
var postData = {
'replyToken': replyToken,
'messages': [{
"type": "flex",
"altText": "This is a carousel",
"contents": {
"type": "carousel",
"contents": contents
}
}]
};
var options = {
'method': 'post',
'headers': headers,
'payload': JSON.stringify(postData),
};
UrlFetchApp.fetch(url, options);
}
第二段程式碼
var onceMaxMessages = 5;
var carouselMaxLoad = 3;
function makeSendMessages(replyData, sendStart, searchContent) {
var replyMessage = [];
var level1 = 0;
var level2 = 0;
var stopPoint = sendStart;
var tempMessage = [];
var sendMessage = [];
if (sendStart == 0) {
var decreaseNum = 2;
} else {
var decreaseNum = 1;
}
while(level1 < onceMaxMessages - decreaseNum && stopPoint < replyData.length) {
tempMessage.push(replyData[stopPoint]);
level2 += 1; stopPoint += 1;
if (level2 == carouselMaxLoad || stopPoint == replyData.length) {
level1 += 1;
level2 = 0;
sendMessage.push(tempMessage);
tempMessage = [];
}
}
if (sendStart == 0) {
replyMessage.push({type:"text", text:"總共找到 " + replyData.length + " 筆「" + searchContent + "」相關的資料"});
if (sendMessage.length > onceMaxMessages - 2) {
var loopEnd = onceMaxMessages - 2;
} else {
loopEnd = sendMessage.length;
}
} else {
if (sendMessage.length > onceMaxMessages - 1) {
var loopEnd = onceMaxMessages - 1;
} else {
loopEnd = sendMessage.length;
}
}
for (l = 0; l < loopEnd; l++) {
replyMessage.push(
{
"type": "flex",
"altText": "搜尋結果",
"contents": {
"type": "carousel",
"contents": sendMessage[l]
}
}
);
}
if (stopPoint < replyData.length) {
replyMessage.push(
{
"type": "flex",
"altText": "顯示更多資料",
"contents": {
"type": "bubble",
"body": {
"type": "box",
"layout": "vertical",
"contents": [
{
"type": "text",
"text": "點選顯示更多資料",
"size": "lg",
"align": "center",
"weight": "bold"
}
],
"action": {
"type": "postback",
"label": "點選顯示更多資料",
"data": "showMore" + searchContent + stopPoint
}
}
}
}
);
}
return replyMessage;
}