2016-09-06 44 views
1

當我在電子表格中插入圖像,然後將其轉換爲pdf時,我遇到了應用程序腳本的問題。該圖像未出現在文檔pdf中。電子表格和應用程序腳本中的圖像問題

我的代碼是:

此處插入圖片

libro=SpreadsheetApp.openById('XXXXXXXXXXXXXXXXX'); 
hojaX=Libro.getSheetByName('Test'); 
hojax.insertImage("http://test.com/imagen.jpg", 3, 71, 0, 0); 

這裏轉換電子表格PDF

var file = DriveApp.getFileById('XXXXXXXXXXXXXXXXX').getAs('application/pdf').getBytes(); 

這裏附加到電子郵件發送

var attach = {fileName:'name.pdf',content:file, mimeType:'application/pdf'}; 

MailApp.sendEmail([email protected], "title", "message" {attachments:[attach]}); 

我無法修復,PD f發送沒有圖像,你能幫助我嗎? 關於

+0

該腳本不完整。請結帳[mcve]。 –

回答

0

以下是如何操作insert an image in App Script

function myFunction() { 
    var SPREADSHEET_URL = 'https://docs.google.com/spreadsheets/d/abcdefghiklmnopqrstopwxyz/edit#gid=0'; 
    var SHEET_NAME = 'Sheet1'; 

    var ss = SpreadsheetApp.openByUrl(SPREADSHEET_URL); 
    var sheet = ss.getSheetByName(SHEET_NAME); 

    var response = UrlFetchApp.fetch(
     'http://i.imgur.com/PnJTXjN.png'); //url of the image 
    var binaryData = response.getContent(); 

    var blob = Utilities.newBlob(binaryData, 'image/png', 'MyImageName'); 
    sheet.insertImage(blob, 1, 1); //this puts the image in A1 of the cell 
} 

這裏是如何的圖像轉換爲PDF格式,並在Amit Agarwal電子郵件禮節重視。當你運行這個代碼時,一封郵件將被髮送到你的郵件,並附有pdf。

function convertImageToPDF() { 

var image = UrlFetchApp.fetch('http://i.imgur.com/DS6bVac.png'); 

    // grab its bytes and base64-encode them. 
    var base64 = Utilities.base64Encode(image.getBlob().getBytes()); 
    var html = '<img src="data:image/png;base64,'+base64+'" />'; 

    // create a blob, convert to PDF 
    var blob = Utilities.newBlob(html, MimeType.HTML).setName('myImage' + ".pdf"); 

    //save to Google Drive 
    var file = { 
    title: 'myImage.pdf', 
    mimeType: 'application/pdf' 
    }; 
    file = Drive.Files.insert(file,blob.getAs(MimeType.PDF)); 
    Logger.log('ID: %s, File size (bytes): %s', file.id, file.fileSize); 

    //send to email as attached file 
    MailApp.sendEmail("[email protected]", "Image to PDF", "", { 
    attachments:blob.getAs(MimeType.PDF) 
    }); 

} 
+0

不工作,我嘗試這個代碼,但PDF文件沒有圖像。 –

相關問題