我在电子表格中创建了一个发票模板,并使用附加的VBA代码从中创建报告(实际上它将某些单元格值存储到另一个工作表中作为表格报告)。我想将其移植到Google Spreadsheet并需要帮助将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 :(得分: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;
}