我正在嘗試將影像圖表從 Google Sheets 發送到 Telegram。我正在將影像圖表保存到 Google 磁盤,然后將其發送到 Telegram chat(幫助我的電報機器人)。
如果我以這種方式將 Google Drive 鏈接發送到電報:首先嘗試:
file_id_0 = "https://drive.google.com/file/d/1kvitP05ofdyT4YtHgNBdjP-sxIFQlpo7/view?usp=drivesdk";
這是第一次嘗試的結果:
在這個網站上,我發現解決了這個問題(抱歉我丟失了鏈接):使用 Google Docs 和 Google Drive 中的 file_id。“/d/ file_id /view”之間鏈接上的檔案 ID 。為了確認這個決定,我獲取了 Google Drive 上其他檔案的 ID 并將其連接起來:
file_id_2 = "1bSSzt5S9SgafeAK7D6dfRiFGECxhSuXo";
sendImage(chatId, "https://docs.google.com/uc?id=" file_id_2);
第二次嘗試結果:
現在我試圖從 Google Drive 檔案中提取 file_id
file.getId()
并將這個 Id 連接到 google docs 鏈接。
第三次嘗試結果:
如果我在除錯程式中獲取鏈接并在瀏覽器中打開它 - 這正確打開(如第二次嘗試)。在我看來,第二個和第三個鏈接上的鏈接沒有區別。至少它們在瀏覽器中正確打開。
我應該如何將此鏈接插入到我的函式中,以免出現問題?
PS:我很抱歉我的英語。我準備給出任何解釋,我希望這些圖片有助于理解我的問題。
完整代碼:
function downloadChart2() {
let chatId = "-xxx";
var sheet = SpreadsheetApp.getActiveSheet();
// Get chart and save it into your Drive
var chart = sheet.getCharts()[0];
var file = DriveApp.createFile(chart.getBlob());
// Set url in one cell and resize the column
sheet.getRange(23, 1).setValue(file.getUrl())
sheet.autoResizeColumn(1);
file.setName("1234");
// first try - successful, but bad view on Telegram
var file_id_0 = file.getUrl();
sendText(chatId, file_id_0);
// second try - successful, but i grab this id on adress panel in my brouser
var file_id_1 = "1cAdAMWFdzZFRgXiM9kbxkzrwVAGpoOIy";
sendImage(chatId, "https://docs.google.com/uc?id=" file_id_1);
// third try - unsuccessful
var file_id_2 = file.getId();
var file_id_3 = "https://docs.google.com/uc?id=" file_id_2; // this link correctly
sendImage(chatId, file_id_3); // but this don't work
}
}
function sendImage(chatId, text, keyBoard) {
let data = {
method: 'post',
payload: {
method: 'sendMessage',
chat_id: String(chatId),
text: text,
parse_mode: 'HTML',
reply_markup: JSON.stringify(keyBoard)
}
}
var caption = "Second";
UrlFetchApp.fetch("https://api.telegram.org/bot" token "/sendPhoto?caption=" encodeURIComponent(caption) "&photo=" encodeURIComponent(text) "&chat_id="
chatId "&parse_mode=HTML");
}
uj5u.com熱心網友回復:
當我看到你的腳本時,我認為創建的影像檔案可能沒有公開共享。我認為這可能是您的問題的原因。那么下面的修改呢?
從:
var file = DriveApp.createFile(chart.getBlob());
到:
var file = DriveApp.createFile(chart.getBlob());
file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
參考:
- 設定共享(訪問型別,權限型別)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/374830.html
