I created an invoice template in the spreadsheet and used the attached VBA code to create a report from it (essentially it stores certain cell values into another sheet as a tabular report).I would like to port this over to Google Spreadsheet and need help in converting the VBA to corresponding JavaScript. Can you help?
Thanks
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
Figured this out, thankfully I've had to do this before.
This moves the data by getting the invoice information via a map that I made as an object so it's easily looped through. The tricky part was getting the PDF of the sheet.
I accomplished this by first scoping the function with Driveapp.getRootFolder() so I can get an oAuth token later. I utilized the URLFetchApp use the spreadsheets export functionality to retrieve a PDF blob. I then take this blob, name it, convert it to a file, and insert it into your drives root folder.
//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;
}
Related
I am able to export a list of JSON objects to an excel file using sheetjs in angular. One of the requirements is, user should be able to sort and filter the date fields like below.
But as you can see, along with the date part, the time part also comes while filtering. Is there a way, I can remove the time part and still be able to do the filtering like above?
Here is my code.
exportToExcel(arrData: any[], fileName: string, formattingData: ExcelFormattingData = undefined) {
const worksheet: XLSX.WorkSheet = XLSX.utils.json_to_sheet(arrData, {cellDates: true, dateNF: this.userAuthToken.dateFormat});
if(formattingData != null) {
const range = XLSX.utils.decode_range(worksheet['!ref']);
for (let j = range.s.c; j <= range.e.c; j++) {
let ref_columnHeader = XLSX.utils.encode_cell({r:range.s.r, c:j});
let columnName = worksheet[ref_columnHeader].v;
if(formattingData.Values.includes(columnName)){
for(let i = range.s.r + 1; i <= range.e.r; ++i) {
let ref = XLSX.utils.encode_cell({r:i, c:j});
if(worksheet[ref] != null) {
worksheet[ref].t = 'd';
worksheet[ref].z = formattingData.Format;
}
}
}
}
}
const workbook: XLSX.WorkBook = { Sheets: { 'data': worksheet }, SheetNames: ['data'] };
const excelBuffer: any = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' });
const blob = new Blob([excelBuffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8' });
FileSaver.saveAs(blob, fileName);
}
I need a little help with a google spreadsheet script. Currently I'm using a script to copy entire row if contition is met(checkbox TRUE/FALSE). If different condition is met(different checkbox), i need to copy that entire row to "Completed" AND the tricky part for me, I need to ONLY copy cells in A,B,P columns in that specific row to sheet "Flash" to position A,B,C.. Could someone help me? Thanks!
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Orders" && r.getColumn() == 9 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Completed");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
} else if(s.getName() == "Orders" && r.getColumn() == 15 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Completed");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
var targetSheet = ss.getSheetByName("Flash");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
s.deleteRow(row);
}
}
I created two custom content types, ProjectContract and PaymentRequest. Under PaymentRequest, I have a reference field Contract which I would like to use to reference ProjectContract. When I am creating/changing PaymentRequest, I need the following:
how can I initialize Content Picker to display ContractNumber field of available ProjectContracts?
how can I display selected ProjectContract's ContractNumber under ReferenceField Grid control?
The SN js code and the mvc contains/returns fix field values. I did not find any setting where I can add custom fields to show.
First of all, what is the version of that SN package, because the oData.svc request will not work on older versions. It is available from 6.2.
About the oData, here is a link: http://wiki.sensenet.com/OData_REST_API
There is another way to solve it, but with this, you need to modify the existion SN codes.
You need to copy (" /Root/Global/scripts/sn/SN.Picker.js ") file into your skin folder with the same structure. (" /Root/Skins/[yourskinfolder]/scripts/sn/SN.ReferenceGrid.js ")
You need to copy (" /Root/Global/scripts/sn/SN.ReferenceGrid.js ") file into your skin folder as well.
Do not modify the original SN file, because it will be overwrite after an SN update.
Next step: copy the following code to line 1068, before the ("$grid.jqGrid({") line, into the InitGrid function.
...
var neededTypeName = "ProjectContract";
var neededFieldName = "ContractNumber";
var findField = false;
o2 = (function () {
var result = [];
var itemArray = [];
$.each(o2, function (index, el) {
el.ContentField = "";
result.push(el);
if (el.ContentTypeName == neededTypeName) {
itemArray.push([index, el.Path]);
findField = true;
}
});
if (findField) {
$.each(itemArray, function (itemIndex, itemElArray) {
var itemId = itemElArray[0];
var itemEl = itemElArray[1];
var thisLength = itemEl.length;
var thislastSplash = itemEl.lastIndexOf("/");
var thisPath = itemEl.substring(0, thislastSplash) + "('" + itemEl.substring(thislastSplash + 1, thisLength) + "')";
$.ajax({
url: "/oData.svc" + thisPath + "?metadata=no$select=Path," + neededFieldName,
dataType: "json",
async: false,
success: function (d) {
result[itemId].ContentField = d.d[neededFieldName];
}
});
});
colNames.splice(6, 0, "ContentField");
colModel.splice(6, 0, { index: "ContentField", name: "ContentField", width: 100 });
return result;
}
return o2;
})();
...
$grid.jqGrid({
...
The "neededTypeName" may contains your content type value, and the "neededFieldName" may contains the field name you want to render.
The other will build up the grid.
This will modify the Content picker table.
You need to add this code into the GetResultDataFromRow function, at line 660 before the return of the function.
...
if (rowdata.ContentField != undefined) {
result.ContentField = rowdata.ContentField;
}
...
This will add the selected item properties from the Content picker to the reference field table.
Then you need to open the SN.ReferenceGrid.js and add the following code into the init function before the "var $grid = $("#" + displayAreaId);"
var neededTypeName = "CustomItem2";
var neededFieldName = "Custom2Num";
var findField = false;
var alreadyAdded = false;
var btnAttr = $("#"+addButtonId).attr("onClick");
if (btnAttr.indexOf(neededTypeName) > -1) {
alreadyAdded = true;
colNames[4].width = 150;
colModel[4].width = 150;
colNames.splice(3, 0, "ContentField");
colModel.splice(3, 0, { index: "ContentField", name: "ContentField", width: 60 });
}
initialSelection = (function () {
var result = [];
var itemArray = [];
$.each(initialSelection, function (index, el) {
el.ContentField = "";
result.push(el);
if (el.ContentTypeName == neededTypeName) {
itemArray.push([index, el.Path]);
findField = true;
}
});
if (findField) {
$.each(itemArray, function (itemIndex, itemElArray) {
var itemId = itemElArray[0];
var itemEl = itemElArray[1];
var thisLength = itemEl.length;
var thislastSplash = itemEl.lastIndexOf("/");
var thisPath = itemEl.substring(0, thislastSplash) + "('" + itemEl.substring(thislastSplash + 1, thisLength) + "')";
$.ajax({
url: "/oData.svc" + thisPath + "?metadata=no$select=Path," + neededFieldName,
dataType: "json",
async: false,
success: function (d) {
result[itemId].ContentField = d.d[neededFieldName];
}
});
});
if (!alreadyAdded) {
colNames.splice(3, 0, "ContentField");
colModel.splice(3, 0, { index: "ContentField", name: "ContentField", width: 100 });
}
return result;
}
return initialSelection;
})();
I hope this will help but the SN version should be helpful.
I'm trying to save all of the sheets on my spreadsheet to google drive as one PDF (ultimately I would like to have them email as well). I'm having trouble saving more than just one of the sheets. I've tried multiple way of doing it. The code below is the best way I've found so far. Again the problem is that it only saves the first page as a PDF, I cant figure out how to get around the delete redundant sheets. All the posts I have seen only want to save 1 page, I have over 24 pages that need to be saved as one PDF. Thanks in advance for your help!
function PDF() {
var sheetName = SpreadsheetApp.getActiveSpreadsheet();
var folderID = "*** Google Drive ID***"; // Folder id to save in a Drive folder.
var ss = SpreadsheetApp.openByUrl(
'https://docs.google.com/spreadsheets/d/***Spreadsheet ID***');
var pdfName = "MAR - " + ss.getRange("A1:A1").getValue(); //Need to set the values to another sheet
var sourceSpreadsheet = SpreadsheetApp.getActive();
var sourceSheet = sourceSpreadsheet.getSheetByName(sheetName);
var folder = DriveApp.getFolderById(folderID);
//Copy whole spreadsheet
var destSpreadsheet = SpreadsheetApp.open(DriveApp.getFileById(sourceSpreadsheet.getId()).makeCopy("tmp_convert_to_pdf", folder))
//delete redundant sheets
var sheets = destSpreadsheet.getSheets();
for (i = 0; i < sheets.length; i++) {
if (sheets[i].getSheetName() != sheetName){
destSpreadsheet.deleteSheet(sheets[i]);
}
}
var destSheet = destSpreadsheet.getSheets()[0];
//repace cell values with text (to avoid broken references)
var sourceRange = sourceSheet.getRange(1,1,sourceSheet.getMaxRows(),sourceSheet.getMaxColumns());
var sourcevalues = sourceRange.getValues();
var destRange = destSheet.getRange(1, 1, destSheet.getMaxRows(), destSheet.getMaxColumns());
destRange.setValues(sourcevalues);
//save to pdf
var theBlob = destSpreadsheet.getBlob().getAs('application/pdf').setName(pdfName);
var newFile = folder.createFile(theBlob);
//Delete the temporary sheet
DriveApp.getFileById(destSpreadsheet.getId()).setTrashed(true);
}
By using Drive API, you can convert from a spreadsheet to a PDF which has all sheets in the spreadsheet. In order to use this, so please enable Drive API on Google API Console as follows.
In the script editor, select Resources > Cloud Platform Project
At the bottom of the dialog, click the link for the Google API Console.
In the console, click into the filter box and type part of the name of the API "Drive API", then click the name once you see it.
On the next screen, click Enable API.
Close the Developers Console and return to the script editor. Click OK in the dialog.
I prepared a sample script for creating PDF file from spreadsheet. Please use this to your script.
Script :
var spreadsheetId = "#####";
var folderId = "#####";
var outputFilename = "#####";
var url = "https://www.googleapis.com/drive/v3/files/" + spreadsheetId + "/export?mimeType=application/pdf";
var options = {
method: "GET",
headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()},
muteHttpExceptions: true
};
var response = UrlFetchApp.fetch(url, options).getBlob();
DriveApp.getFolderById(folderId).createFile(response).setName(outputFilename);
About this script, although I confirmed this works fine, if it doesn't work at your environment, please tell me. And if I misunderstand your question, I'm sorry.
Added 1 :
function PDF() {
var sheetName = SpreadsheetApp.getActiveSpreadsheet();
var folderID = "*** Google Drive ID***"; // Folder id to save in a Drive folder.
var ss = SpreadsheetApp.openByUrl(
'https://docs.google.com/spreadsheets/d/***Spreadsheet ID***');
var pdfName = "MAR - " + ss.getRange("A1:A1").getValue(); //Need to set the values to another sheet
var sourceSpreadsheet = SpreadsheetApp.getActive();
var sourceSheet = sourceSpreadsheet.getSheetByName(sheetName);
var folder = DriveApp.getFolderById(folderID);
//Copy whole spreadsheet
var destSpreadsheet = SpreadsheetApp.open(DriveApp.getFileById(sourceSpreadsheet.getId()).makeCopy("tmp_convert_to_pdf", folder))
//delete redundant sheets
var sheets = destSpreadsheet.getSheets();
for (i = 0; i < sheets.length; i++) {
if (sheets[i].getSheetName() != sheetName){
destSpreadsheet.deleteSheet(sheets[i]);
}
}
var destSheet = destSpreadsheet.getSheets()[0];
//repace cell values with text (to avoid broken references)
var sourceRange = sourceSheet.getRange(1,1,sourceSheet.getMaxRows(),sourceSheet.getMaxColumns());
var sourcevalues = sourceRange.getValues();
var destRange = destSheet.getRange(1, 1, destSheet.getMaxRows(), destSheet.getMaxColumns());
destRange.setValues(sourcevalues);
//save to pdf
// var theBlob = destSpreadsheet.getBlob().getAs('application/pdf').setName(pdfName);
// var newFile = folder.createFile(theBlob);
// A sample script was added here.
var url = "https://www.googleapis.com/drive/v3/files/" + destSpreadsheet.getId() + "/export?mimeType=application/pdf";
var options = {
method: "GET",
headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()},
muteHttpExceptions: true
};
var response = UrlFetchApp.fetch(url, options).getBlob();
DriveApp.getFolderById(folderID).createFile(response).setName(pdfName);
//Delete the temporary sheet
DriveApp.getFileById(destSpreadsheet.getId()).setTrashed(true);
}
Added 2 :
function PDF() {
var folderID = "*** Google Drive ID***"; // Folder id to save in a Drive folder.
var ss = SpreadsheetApp.openByUrl(
'https://docs.google.com/spreadsheets/d/***Spreadsheet ID***');
var pdfName = "MAR - " + ss.getRange("A1:A1").getValue(); //Need to set the values to another sheet
var sourceSpreadsheet = SpreadsheetApp.getActive();
var folder = DriveApp.getFolderById(folderID);
// A sample script was added here.
var url = "https://www.googleapis.com/drive/v3/files/" + sourceSpreadsheet.getId() + "/export?mimeType=application/pdf";
var options = {
method: "GET",
headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()},
muteHttpExceptions: true
};
var response = UrlFetchApp.fetch(url, options).getBlob();
DriveApp.getFolderById(folderID).createFile(response).setName(pdfName);
}
Here is the function that creates PDF file from google sheet and function that moves new created file into the folder that id you give to the parameter of the function.
function downloadPDF(fileId, folderId) {
var file = Drive.Files.get(fileId);
var url = file.exportLinks[MimeType.PDF];
var options = {
headers: {
Authorization:"Bearer " + ScriptApp.getOAuthToken()
},
muteHttpExceptions : true
}
var response = UrlFetchApp.fetch(url, options);
var status = response.getResponseCode();
var result = response.getContentText();
if (status != 200) {
// Get additional error message info, depending on format
if (result.toUpperCase().indexOf("<HTML") !== -1) {
var message = strip_tags(result);
}
else if (result.indexOf('errors') != -1) {
message = JSON.parse(result).error.message;
}
throw new Error('Error (' + status + ") " + message );
}
var doc = response.getBlob();
var newFileid = DriveApp.createFile(doc).setName(file.title + '.pdf').getId();
let id = moveFileTo(newFileid, folderId);
return id;
}
function moveFileTo(sourceId, folderId){
let file = DriveApp.getFileById(sourceId)
let blob = file.getBlob();
let id = DriveApp.getFolderById(folderId).createFile(blob).getId();
file.setTrashed(true)
return id;
}
I am creating an SAPUI5 app that needs to upload attachments. I still am very new use SAPUi5. I want to save the uploaded file to the DB. Could I use the Document service? If so please provide me with docs or tutorials to do so.
I have done that in one off my SAPUI5 apps, for the uploading function check out my gist.
Or check this for reference
onUpload: function(e) {
var fU = this.getView().byId("idfileUploader");
var domRef = fU.getFocusDomRef();
var file = domRef.files[0];
var reader = new FileReader();
reader.onload = function(oEvent) {
var strCSV = oEvent.target.result;
var arrCSV = strCSV.match(/[\w .]+(?=,?)/g);
var noOfCols = 6;
var headerRow = arrCSV.splice(0, noOfCols);
var data = [];
while (arrCSV.length > 0) {
var obj = {};
var row = arrCSV.splice(0, noOfCols);
for (var i = 0; i < row.length; i++) {
obj[headerRow[i]] = row[i].trim();
}
data.push(obj);
}
};
reader.readAsBinaryString(file);
}
Irrespective of whether you use DB or Document Service, with respect to SAPUI5,
you can use sap.ui.unified.FileUploader. Read more here
Sample XML code would be:
<sap.ui.unified:FileUploader
id="fileUploader"
width="100%"
uploadUrl=""
placeholder="Add attachment"
uploadOnChange="false"
uploadComplete="handleUploadComplete"
change="handleValueChange"
typeMissmatch="handleTypeMissmatch"
style="Emphasized"
useMultipart="false" >
</sap.ui.unified:FileUploader>
While uploading make sure, you add slug and x-csrf-token to the headerparameters.
Example:
var oFileUploader = _this.byId("fileUploader");
oFileUploader.addHeaderParameter(new sap.ui.unified.FileUploaderParameter({
name: "slug",
value: oFileUploader.getValue()
}));
oFileUploader.addHeaderParameter(new sap.ui.unified.FileUploaderParameter({
name: "x-csrf-token",
value: _this.oDataModel.getSecurityToken()
}));
oFileUploader.upload();
And if you are using ABAP Netweaver gateway stack, you need to implement CREATE_STREAM method in DPC_EXT classes. Also need to make sure that pareticular EventType with "media" supported in Gateway model.
uploadFile:function{
var that = this;
var count = 0;
var o = "<URI>";
var h = new sap.ui.model.odata.ODataModel(o, true);
var d = "";
h.setSizeLimit(5000);
h.forceNoCache(true);
var b = ({
"X-Requested-With": "XMLHttpRequest",
"Content-Type": "application/json",
"X-CSRF-Token": "Fetch",
});
OData.request({
requestUri: o,
method: "GET",
headers: b
}, function (e, j) {
d = j.headers["x-csrf-token"];
that.csrfToken = d;
that.getModel("oAppData").setProperty("/busyIndicators/uploadFile", true);
for (var l = 0; l < that.fileDataAD.length; l++) {
var i = "<URI>";
var h = new sap.ui.model.odata.ODataModel(i, true);
h.setSizeLimit(5000);
h.forceNoCache(true);
var b = ({
"X-CSRF-Token": that.csrfToken,
"Slug": that.fileDataAD[l].fileName + "|" + that.fileDataAD[l].fileType + "|" + "B" + "|" + reqId + "|" + 1
});
OData.request({
requestUri: i,
method: "POST",
headers: b,
data: that.fileDataAD[l].file
}, function (oData, oRes) {
count = count + 1;
if (count === that.fileDataAD.length) {
that._uploadCompleteAddDependent();
}
}.bind(this), function (oError) { }
}
});
}
where fileDataAD is buffer array