2016-04-26 80 views
0

我在電子表格中創建了一個發票模板,並使用附加的VBA代碼從中創建報告(實質上,它將某些單元格值存儲爲另一個表格作爲表格報告)。我想將這個Google電子表格「,並需要幫助將VBA轉換爲相應的JavaScript。你能幫我嗎?來自發票模板的報告

感謝

Sub InvoiceReport() 
    Dim myFile As String, lastRow As Long 
    myFile = 「C: \invoices\」 & Sheets(「Sheet1」).Range(「B5」) & 「_」 & Sheets(「Sheet1」).Range(「F1」) & Format(Now(), 「yyyy - mm - dd」) & 「.pdf」 
    lastRow = Sheets(「Sheet2」).UsedRange.SpecialCells(xlCellTypeLastCell).Row + 1 

    ‘ Transfer data to sheet2 
    Sheets(「Sheet2」).Cells(lastRow, 1) = Sheets(「Sheet1」).Range(「B5」) 
    Sheets(「Sheet2」).Cells(lastRow, 2) = Sheets(「Sheet1」).Range(「F1」) 
    Sheets(「Sheet2」).Cells(lastRow, 3) = Sheets(「sheet1」).Range(「I36」) 
    Sheets(「Sheet2」).Cells(lastRow, 4) = Now 
    Sheets(「Sheet2」).Hyperlinks.Add Anchor: = Sheets(「Sheet2」).Cells(lastRow, 5), Address: = myFile, TextToDisplay: = myFile‘ Create invoice in PDF format 
    Sheets(「sheet1」).ExportAsFixedFormat Type: = xlTypePDF, Filename: = myFile 
    Application.DisplayAlerts = False 

    ‘ create invoice in XLSX format 
    ActiveWorkbook.SaveAs「 C: \invoices\」 & Sheets(「Sheet1」).Range(「B5」) & 「_」 & Sheets(「Sheet1」).Range(「F1」) & 「_」 & Format(Now(), 「yyyy - mm - dd」) & 「.xlsx」, FileFormat: = 51‘ ActiveWorkbook.Close 
    Application.DisplayAlerts = True 

End Sub 
+0

這個問題有點寬泛。將表格保存爲PDF是一項挑戰,將這些值複製到另一個表格是另一個挑戰。我會在兩個不同的問題中一次解決這個問題,只有在試圖自己解決這個問題之後才能解決這個問題。 SO上還有許多與將PDF表格導出爲PDF有關的其他答案。 –

+0

感謝道格拉斯,我已經更新我的查詢,以更具體和我目前使用的代碼。希望這增加了清晰度。 – user6254079

+0

還有一件事,對於傳輸,您可以與您的預期輸出是什麼樣的數據集共享一個電子表格?讓回答你的問題的人更容易,所以他們不必開始假設你的數據是什麼樣的。 –

回答

0

想通了這一點,幸運的是我有之前做到這一點。

這樣做是通過通過我作爲對象製作的地圖獲取發票信息來移動數據,因此它很容易循環。棘手的部分是獲得工作表的PDF。

我通過第一個作用範圍Driveapp.getRootFolder()來完成此任務,以便稍後可以獲取oAuth標記。我利用URLFetchApp使用電子表格導出功能來檢索PDF blob。然後我拿起這個blob,命名它,將它轉換爲一個文件,然後將它插入你的驅動器根文件夾。

//Data mapping for the invoice itself 
var invoiceDetailsMap = { 
    'Buyer Name': { 
    rowIndex: 4, 
    columnIndex: 1 
    }, 
    'Invoice Number': { 
    rowIndex: 0, 
    columnIndex: 5  
    }, 
    'Total Amount': { 
    rowIndex: 35, 
    columnIndex: 7  
    }, 
    'Date & Time': { 
    rowIndex: 0, 
    columnIndex: 1  
    } 
} 

//Entry point for script 
function EntryPoint() { 
    var spreadsheet = SpreadsheetApp.getActive() 
    var sheet = spreadsheet.getSheetByName('Sheet1'); 
    var dataRange = sheet.getDataRange(); 
    var valuesRange = dataRange.getValues(); 

    var invoiceData = GetInvoiceDetails(valuesRange); 
    WriteInviceDetailsToSheet(invoiceData); 
    GetPDF(spreadsheet, invoiceData['File Name']); 
} 

//Writes the invoice details to the 2nd sheet 
function WriteInviceDetailsToSheet(invoiceData){ 
    var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet2'); 
    var dataRange = sheet.getDataRange(); 
    var valuesRange = dataRange.getValues(); 
    var columns = GetColumns(valuesRange, dataRange.getNumColumns(), 0); 

    var arrayToWrite = [[]]; 
    for(var column in columns.columns){ 
    if(typeof invoiceData[column] !== 'undefined'){ 
     arrayToWrite[0].push(invoiceData[column]); 
    } 
    } 
    sheet.insertRowAfter(dataRange.getLastRow()); 
    sheet.getRange(dataRange.getLastRow() + 1, 1, 1, arrayToWrite[0].length).setValues(arrayToWrite); 
} 

//Gets the invoice details absed on the mappings 
function GetInvoiceDetails(valuesRange) { 
    var output = {}; 
    for(var value in invoiceDetailsMap){ 
    output[value] = valuesRange[invoiceDetailsMap[value].rowIndex][invoiceDetailsMap[value].columnIndex]; 
    } 
    output['File Name'] = 'Invoice' + output['Invoice Number'] + '_' + FormatDate(output['Date & Time'], 'MM.dd.yyyy'); 
    return output; 
} 

//Gets the PDF and inserts it into drive 
function GetPDF(spreadsheet, fileName){ 
    DriveApp.getRootFolder(); //Scoping 
    var urlParameters = 'export?exportFormat=pdf&format=pdf&size=letter&portrait=true&fitw=true&source=labnol&sheetnames=false&printtitle=false&pagenumbers=false&gridlines=false&fzr=false&gid=0'; 
    var baseURL = spreadsheet.getUrl(); 
    baseURL = baseURL.replace(/edit$/,''); 
    var response = UrlFetchApp.fetch(baseURL + urlParameters, { 
       headers: {'Authorization': 'Bearer ' + ScriptApp.getOAuthToken() } 
       }); 

    var pdfBlob = response.getBlob().setName(fileName + '.pdf'); 
    var file = DriveApp.createFile(pdfBlob); 
    DriveApp.addFile(file); 
} 


//Reformts a date 
function FormatDate(date, format) 
{ 
    var temp = new Date(date); 
    var output = Utilities.formatDate(temp, "PST", format); 
    return output; 
} 

//Gets a columns object for the sheet for easy indexing 
function GetColumns(valuesRange, columnCount, rowIndex) 
{ 
    var columns = { 
    columns: {}, 
    length: 0 
    } 

    Logger.log("Populating columns..."); 
    for(var i = 0; i < columnCount; i++) 
    { 
    if(valuesRange[0][i] !== ''){ 
     columns.columns[valuesRange[0][i]] = {index: i ,value: valuesRange[0][i]}; 
     columns.length++;  
    } 
    } 
    return columns; 
}