I am writing Protractor e2e testing .I need to Dynamically read the values from Excel .Can anyone help me out from this..
Inititally add xlsjs to node, and then try this code
var sheetNumber = 0;
//Define file Path name
var path = require('path');
var fileNamePath = path.resolve(__dirname, 'E:/excel.xls');
//NodeJs read file
var XLS;
if (typeof require !== 'undefined') {
XLS = require('E:/node/node_modules/xlsjs');
}
//Working with workbook
var workbook = XLS.readFile(fileNamePath);
var sheetNamelist = workbook.SheetNames;
var value = workbook.Sheets[sheetNamelist[sheetNumber]][cellId].v;
In the above sheet number means, in the Excel sheet your data is located in which sheet and cellId is in that sheet which shell data do you want. your data was stored in the variable value, you can use that data in any type.
Related
This seems like a simple problem, add 4 Form Event columns(Cost 1, Cost 2, Cost 3, Cost 4) and output calculated 'Total' to a PDF via email utilizing a template. I have no problem getting the Event(e) data to the template and sent via email but cannot get the total. I've tried many different Google App Scripts from this site to no avail. Currently, I have a separate sheet called 'Total' and created an array that captures the Event data columns and displays exactly what I want but I cannot get it to the template, problem code may be on line 20. I've included links to the template as well as the Spreadsheet and the Google App Script
// Get template from Google Docs and name it
var docTemplate = "1Ti1n71wpA-U5X9yLqSIfLC9VXqcxOGGsZQhYq0ZwJX4"; // *** replace with your template ID ***
var docName = "Calculate the total";
// When Form Gets submitted
function onFormSubmit(e) {
var name = "Rick"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Total'); //Get 'Total' sheet
var row = sheet.getLastRow(); //Get 'Total' last row
//Get information from form and set as variables
var todaysDate = Utilities.formatDate(new Date(), "CST", "MM/dd/yyyy, hh:mm");
var email_address = "MyEmail address";
var cost1 = e.values[1];
var cost2 = e.values[2];
var cost3 = e.values[3];
var cost4 = e.values[4];
var total = sheet.getRange(row, [1]).getValue(); //Is this the problem?
// Logger.log(e.namedValues);
// Get document template, copy it as a new temp doc, and save the Documents ID
var copyId = DriveApp.getFileById (docTemplate)
.makeCopy(docName + ' for '+ name)
.getId();
// Open the temporary document
var copyDoc = DocumentApp.openById(copyId);
// Get the documents body section
var copyBody = copyDoc.getActiveSection();
// Replace place holder keys,in our google doc template
copyBody.replaceText('<<name>>', name);
copyBody.replaceText('<<cost1>>', cost1);
copyBody.replaceText('<<cost2>>', cost2);
copyBody.replaceText('<<cost3>>', cost3);
copyBody.replaceText('<<cost4>>', cost4);
copyBody.replaceText('<<total>>', total);
copyBody.replaceText('<<timeStamp>>', todaysDate);
// Save and close the temporary document
copyDoc.saveAndClose();
// Convert temporary document to PDF by using the getAs blob conversion
var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");
// Attach PDF and send the email
var subject = "Your Total Cost Project Script";
var body = name + ", here is the total cost for your project ";
MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: pdf});
// Delete temp file
DriveApp.getFileById(copyId).setTrashed(true);
}
Spreadsheet - https://docs.google.com/spreadsheets/d/144t33X98eZIAH2k5hCA--fFeUzmJCGefKI7lC1EE4Xc/edit?usp=sharing
Template - https://docs.google.com/document/d/1Ti1n71wpA-U5X9yLqSIfLC9VXqcxOGGsZQhYq0ZwJX4/edit?usp=sharing
I am working on AngularJs application testing framework where I am using Protractor. I want to read the data (urls, usernames, passwords) from an excel sheet. I am using the following code but it's showing me errors.
Please find the below code:
var Excel = require('exceljs');
var wrkbook = new Excel.Workbook();
wrkbook.xlsx.readFile('E:\\Login_Data.xlsx').then(function()
{
var worksheet = wrkbook.getWorksheet('Sheet1');
worksheet.eachRow(function (Row, Test_URL)
{
console.log("Row " + Test_URL + " = " + JSON.stringify(Row.User_Name));
});
});
The data from excel sheet is :
Test_URL User_Name Password
http://...com abc#1111 xyz#333
Please let me know your positive inputs so that I can run my code and proceed forward.
Thanks in advance
eachRow isn't getting the Test_URL variable that you set as function parameter; that is instead the row index.
For getting every value of the row, you can use Row.values, and also you could the value of each Cell (corresponding to that row) with .getCell.
So it should be something like this:
var Excel = require('exceljs');
var wrkbook = new Excel.Workbook();
wrkbook.xlsx.readFile('E:\\Login_Data.xlsx').then(function()
{
var worksheet = wrkbook.getWorksheet('Sheet1');
worksheet.eachRow(function (Row, rowIndex)
{
var test_url = Row.getCell(1).value;
var user_name = Row.getCell(2).value;
var password = Row.getCell(3).value;
// do whatever you want with those variables.
});
});
Tried with
cellValue= worksheet.getRow(1).getCell(1).value;
and
cellValue=console.log(worksheet.getCell('A1'))
please find below my code :
cellread2=function(){
var Excel;
var filePath = path.resolve(__dirname,'E:/excel.xlsx');
if (typeof require !== 'undefined') {
Excel = require('C:/Users/user/AppData/Roaming/npm/node_modules/exceljs/dist/exceljs');
}
var wb = new Excel.Workbook();
console.log(wb);
wb.xlsx.readFile(filePath);
var worksheet = wb.getWorksheet('Sheet1');
//cellValue= worksheet.getRow(1).getCell(1).value;//Error :TypeError: Cannot read property 'getRow' of undefined
cellValue=console.log(worksheet.getCell('A1'))// TypeError: Cannot read property 'getCell' of undefined
console.log(cellValue);
}
There is a problem with the existing library code. If you want to make it work you will have to make few changes in the existing code in file AppData\Roaming\npm\node_modules\exceljs\dist\es5\xlsx\xform\sheet\worksheet-xform.js. Replace the code at line 284 with the following :
if (drawing.anchors && drawing.anchors.length > 0) {
drawing.anchors.forEach(function (anchor) {
if (anchor.medium && anchor.range) {
var image = {
type: 'image',
imageId: anchor.medium.index,
range: anchor.range
};
model.media.push(image);
}
});
}
And as for the code for reading the file use the following code.
Note: I installed exceljs library globally and I am using version 4.6.1
var path = require('path');
var Excel = require('exceljs');
var cellread2 = function () {
var filePath = path.resolve('E:', 'excel.xlsx');
var wb = new Excel.Workbook();
wb.xlsx.readFile(filePath).then(function (data) {
var worksheet = wb.getWorksheet('Sheet1');
var cellValue = worksheet.getRow(1).getCell(1).value;
console.log('cellValue', cellValue);
}).catch(function (e) {
console.log(e);
});
}
cellread2();
It may occurs due to objects are mapped into the excel sheet instead of textual data.
Please create another excel sheet and check it again will resolve this issue.'
Sample code to read excel sheet using node.js is given below,
var Excel = require('exceljs');
var wb = new Excel.Workbook();
var path = require('path');
var filePath = path.resolve(__dirname,'sample.xlsx');
wb.xlsx.readFile(filePath).then(function(){
var sh = wb.getWorksheet("Sheet1");
sh.getRow(1).getCell(2).value = 32;
wb.xlsx.writeFile("sample2.xlsx");
console.log("Row-3 | Cell-2 - "+sh.getRow(3).getCell(2).value);
console.log(sh.rowCount);
//Get all the rows data [1st and 2nd column]
for (i = 1; i <= sh.rowCount; i++) {
console.log(sh.getRow(i).getCell(1).value);
console.log(sh.getRow(i).getCell(2).value);
}
});
In my case the sheet name did not match with 'Sheet1', since I was modifying an existing excel file. Replacing the sheet name resolved the issue.
I need a script to be able to create the form response spreadsheet of a Google Form and name that Spreadsheet the same as the Form + (Responses) at the end of the name. I have no idea how to do this. I am guessing it has to do with the script below, but the script does not understand that "Title" is the same as a "Name". (I do not know how to append the "(Responses)" part at the end either.) Any help would be appreciated.
function myFunction() {
var form = FormApp.openById('FORM ID HERE').getTitle();
var ss = SpreadsheetApp.create(form);
form.setDestination(FormApp.DestinationType.SPREADSHEET, ss.getId());
}
I found the answer and also how to apply it to many forms in a folder. The answer is below.
function myFunction() {
var files = DriveApp.getFolderById("0B6Eeub3cEBoobnpxWXdjSWxJRm8").getFiles()
while (files.hasNext()) {
var file = files.next();
var form = FormApp.openById(file.getId());
var formName = DriveApp.getFileById(file.getId()).getName();
var ss = SpreadsheetApp.create(formName + ' (Responses)');
form.setDestination(FormApp.DestinationType.SPREADSHEET, ss.getId());
}
}
I have created SAPUI5 application, in that I have loaded data from external .xml file into a table, it was fine. Now, I am trying to delete a specific row from that table.
For this purpose, I use this code:
var oModel = new sap.ui.model.xml.XMLModel();
oModel.loadData("Deployments.xml", "", false);
sap.ui.getCore().setModel(oModel);
oTable.bindRows("/service"); // here "service" is the root element of xml file
var oTable = new sap.ui.commons.Button({
text: "Delete Service",
press: function() {
var idx = oTable.getSelectedIndex();
if (idx !== -1) {
var m = oTable.getModel();
var data = m.getData();
var removed = data.splice(idx, 1); // error showing at this line
m.setData(data);
sap.m.MessageToast.show(JSON.stringify(removed[0]) + 'is removed');
} else {
sap.m.MessageToast.show('Please select a row');
}
}
});
But, I am getting error at the line: var removed = data.splice(idx, 1);. However, the same code is good for when model is JSON. How can I delete a specific row from a table when model XMLModel?
It is a lot easier an more reliable to use a Bindings BindingPath to manipulate data belonging to a particular binding. Here is your adapted sample for a XMLModel:
press: function() {
var iIdx = oTable.getSelectedIndex();
var sPath = oTable.getContextByIndex(iIdx).getPath();
var oObj = oTable.getModel().getObject(sPath);
oObj.remove();
oTable.getModel().refresh();
}
This way you save the hazzle of dealing with the XML structure and furthermore this will scale with any change in the binding path you might introduce in the future.
BR
Chris
var data = m.getData();
data is not an Array. It is a XML document.
To remove an entry from the document:
var root = data.childNodes[0];
var aEntry = root.getElementsByTagName("entry");
root.removeChild(aEntry[idx]);