Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将PDF从Google Drive转换为图像,并通过Bot发送到电报_Pdf_Google Apps Script_Google Sheets_Telegram_Telegram Bot - Fatal编程技术网

将PDF从Google Drive转换为图像,并通过Bot发送到电报

将PDF从Google Drive转换为图像,并通过Bot发送到电报,pdf,google-apps-script,google-sheets,telegram,telegram-bot,Pdf,Google Apps Script,Google Sheets,Telegram,Telegram Bot,目前,我使用的逐步方法如下: 第一步->从我的电子表格创建页面的PDFJogos na TV: function CreatePDF() { var ss = SpreadsheetApp.getActive(); SpreadsheetApp.flush(); var theurl = 'https://docs.google.com/a/mydomain.org/spreadsheets/d/' + 'ID CODE TO SPREADSHEET' +

目前,我使用的逐步方法如下:

第一步->从我的电子表格创建页面的PDF
Jogos na TV

function CreatePDF() {
  var ss = SpreadsheetApp.getActive();
  SpreadsheetApp.flush();
  var theurl = 'https://docs.google.com/a/mydomain.org/spreadsheets/d/' +
      'ID CODE TO SPREADSHEET' +
        '/export?format=pdf' +
          '&size=0' +
            '&portrait=true' +
              '&fitw=true' + 
                '&top_margin=0' +            
                  '&bottom_margin=0' +         
                    '&left_margin=0' +        
                      '&right_margin=0' +     
                        '&sheetnames=false&printtitle=false' +
                          '&pagenum=false' +
                            '&gridlines=false' +
                              '&fzr=FALSE' +
                                '&gid=' +
                                  'ID CODE TO SPREADSHEET PAGE';
  
  var token = ScriptApp.getOAuthToken();
  var docurl = UrlFetchApp.fetch(theurl, { headers: { 'Authorization': 'Bearer ' +  token } });
  var pdfBlob = docurl.getBlob();
  
  //...get token and Blob (do not create the file);
  
  var fileName = ss.getSheetByName("Jogos na TV").getRange("A1").getValue();
  
  //Access or create the 'PDF' folder;
  var folder;
  var folders = DriveApp.getFoldersByName("PDF");
  if(folders.hasNext()) {
      folder = folders.next();
    }else {
      folder = DriveApp.createFolder("PDF");
    }
  
  //Remove duplicate file with the same name;
  var existing = folder.getFilesByName(fileName);
  if(existing.hasNext()) {
      var duplicate = existing.next();
      if (duplicate.getOwner().getEmail() == Session.getActiveUser().getEmail()) {
        var durl = 'https://www.googleapis.com/drive/v3/files/'+duplicate.getId();
        var dres = UrlFetchApp.fetch(durl,{
          method: 'delete',
          muteHttpExceptions: true,
          headers: {'Authorization': 'Bearer '+token}
        });
        var status = dres.getResponseCode();
        if (status >=400) {
          
        } else if (status == 204) {
          folder.createFile(pdfBlob.setName(fileName));
        }
      }
    } else {
      folder.createFile(pdfBlob.setName(fileName));
    }
}
=EnviarTelegram("botSecret","chatId","Url to PDF","Programação de jogos na TV

"&TEXT('Jogos Hoje'!B1,"DD/MM/YYYY")&" e "&TEXT('Jogos Amanhã'!B1,"DD/MM/YYYY"))
第二步->手动复制在Google Drive中创建的PDF链接

第三步->我用电报将带有PDF格式的文本发送给我的小组:

function EnviarTelegram(botSecret, chatId, photoUrl, caption) {
  var response = UrlFetchApp.fetch("https://api.telegram.org/bot" + botSecret + "/sendPhoto?caption=" + encodeURIComponent(caption) + "&photo=" + encodeURIComponent(photoUrl) + "&chat_id=" + chatId + "&parse_mode=HTML");
}
通过电子表格发送到电报的当前公式:

function CreatePDF() {
  var ss = SpreadsheetApp.getActive();
  SpreadsheetApp.flush();
  var theurl = 'https://docs.google.com/a/mydomain.org/spreadsheets/d/' +
      'ID CODE TO SPREADSHEET' +
        '/export?format=pdf' +
          '&size=0' +
            '&portrait=true' +
              '&fitw=true' + 
                '&top_margin=0' +            
                  '&bottom_margin=0' +         
                    '&left_margin=0' +        
                      '&right_margin=0' +     
                        '&sheetnames=false&printtitle=false' +
                          '&pagenum=false' +
                            '&gridlines=false' +
                              '&fzr=FALSE' +
                                '&gid=' +
                                  'ID CODE TO SPREADSHEET PAGE';
  
  var token = ScriptApp.getOAuthToken();
  var docurl = UrlFetchApp.fetch(theurl, { headers: { 'Authorization': 'Bearer ' +  token } });
  var pdfBlob = docurl.getBlob();
  
  //...get token and Blob (do not create the file);
  
  var fileName = ss.getSheetByName("Jogos na TV").getRange("A1").getValue();
  
  //Access or create the 'PDF' folder;
  var folder;
  var folders = DriveApp.getFoldersByName("PDF");
  if(folders.hasNext()) {
      folder = folders.next();
    }else {
      folder = DriveApp.createFolder("PDF");
    }
  
  //Remove duplicate file with the same name;
  var existing = folder.getFilesByName(fileName);
  if(existing.hasNext()) {
      var duplicate = existing.next();
      if (duplicate.getOwner().getEmail() == Session.getActiveUser().getEmail()) {
        var durl = 'https://www.googleapis.com/drive/v3/files/'+duplicate.getId();
        var dres = UrlFetchApp.fetch(durl,{
          method: 'delete',
          muteHttpExceptions: true,
          headers: {'Authorization': 'Bearer '+token}
        });
        var status = dres.getResponseCode();
        if (status >=400) {
          
        } else if (status == 204) {
          folder.createFile(pdfBlob.setName(fileName));
        }
      }
    } else {
      folder.createFile(pdfBlob.setName(fileName));
    }
}
=EnviarTelegram("botSecret","chatId","Url to PDF","Programação de jogos na TV

"&TEXT('Jogos Hoje'!B1,"DD/MM/YYYY")&" e "&TEXT('Jogos Amanhã'!B1,"DD/MM/YYYY"))
为PDF创建的缩略图被剪切,因此发送到电报的图像也被剪切,电子表格不能保存为图像,只能保存为文档或PDF

有没有办法自动将PDF转换为图像并发送到电报?

问题和解决方法: 不幸的是,在GoogleApps脚本的方法中没有直接将PDF格式转换为图像数据的方法。因此,在本例中,我认为需要使用变通方法来实现您的目标

解决方法1: 在此解决方案中,使用了外部API。当您想要直接将PDF数据转换为图像数据时,使用外部API如何

您可以在上看到此示例脚本

解决方法2: 在此解决方法中,图纸范围将导出为图像。当我看到您的电子表格时,似乎Google电子表格中的工作表数据范围被导出为PDF数据。在这种情况下,作为另一种解决方法,如何将电子表格的范围转换为图像

您可以在上看到此示例脚本

解决方案3: 在此解决方案中,PDF数据通过
sendDocument
发送。在这种情况下,似乎需要将数据作为
多部分/表单数据
发送。示例脚本如下所示

示例脚本:
var url = "https://api.telegram.org/bot" + botSecret + "/sendDocument?chat_id=" + chatId;
var blob = DriveApp.getFileById("### file ID of PDF file ###").getBlob();
var res = UrlFetchApp.fetch(url, {method: "post", payload: {document: blob}});
console.log(res.getContentText())
参考资料:
  • 相关线程。