Junk contents from formula based cell using JXL api in JAVA - jxl

I am facing a problem while getting contents of cell using JXL api, if I am using formula
in xls sheet. Right now I am using formula - IF($L10="","",+ROUND($L10*1.375,3)) in each
cell but when the cell value is blank i am getting junk charactes when i call cell.getContents() method, the code snip is as follows - >>
Workbook = Workbook.getWorkbook(p_sourceFile);
excelsheet = workbook.getSheet(0);
for (int row = 1; row < noOfRows; row++)
{
cell = excelsheet.getCell(col, row);
content = cell.getContents();
System.out.println("content-" + content); //Is giving me junk character ? when the cell value is blank.
...
It will be a great help if anyone can help me !!!
Regards,
Amit

Try this:
NumberFormulaCell cell = (NumberFormulaCell) excelsheet.getCell(col, row);
content = cell.getValue();
From the JavaDocs for Cell.getContents():
Quick and dirty function to return the contents of this cell as a string. For more complex manipulation of the contents, it is necessary to cast this interface to correct subinterface
The necessary subinterface for a numerical formula is NumberFormulaCell. If you want to get the formula as a String then call cell.getFormula().
I'm not sure if I'm really answering your question here or not. If I'm not, could you post these junk characters that get printed out, please?

Related

Datatable row.add().draw() only displays first letter

I'm having difficulty in display my data in datatable.
So at first I have a sample data in the datatable and then when a button is click it will clear() the datatable and this is where will I add the rows please see below code of lines.
myTable.clear();
for (var i = 0; i < data.response.length; i++) {
console.log(data.response[i]['username']);
myTable.row.add(data.response[i]['username']).draw();
}
When checking what's inside data.response[i]['username'] it has the correct text but when added in row.add() it only displays the first letter. Thank you in advance for your help.
---Update---
So I tried to add random letters instead to see if its only in my array but not
I used thismyTable.row.add('asdqwe').draw(); it returns the first character as well.

MigraDoc Formatting

I am completely new to PDF creation including MigraDoc. I have gotten this far, which is really close to what I want for now. My question is that the text string (myMessage) that I pass to the "bodyParagraph" is up to 100 lines long, which causes three pages to be created, which is good. However the first page's Top margin is slightly greater than the second and third pages. I have no idea of why...
Basically, I am trying to create every page the same. Same header, footer and the body to take the same space regardless of the number of lines in the "bodyParagraph" content. If I have taken the completely wrong approach I would be open to suggestions.
Also, if there is a good tutorial to point me to that would be great. I can't really find anything but samples. I have learned everything from the samples, but sections, paragraph, etc is all new to me and I would like to get a better understanding of what I've done.
public static Document CreateWorkOrderPDF2(Document document, string filename, string WorkOrderHeader, string myMessage)
{
Section section = document.AddSection();
section.PageSetup.PageFormat = PageFormat.Letter;
section.PageSetup.StartingNumber = 1;
section.PageSetup.LeftMargin = 40;
//Sets the height of the top margin
section.PageSetup.TopMargin = 100;
section.PageSetup.RightMargin = 40;
section.PageSetup.BottomMargin = 40;
//MARGIN
HeaderFooter header = section.Headers.Primary;
header.Format.Font.Size = 16;
header.Format.Font.Color = Colors.DarkBlue;
MigraDoc.DocumentObjectModel.Shapes.Image headerImage = header.AddImage("../../Fonts/castorgate.regular.png");
headerImage.Width = "2cm";
Paragraph headerParagraph = section.AddParagraph();
headerParagraph = header.AddParagraph(WorkOrderHeader);
//BODY PARAGRAPH
Paragraph bodyParagraph = section.AddParagraph();
bodyParagraph = section.AddParagraph(myMessage);
bodyParagraph.Format.Font.Size = 10;
bodyParagraph.Format.Font.Color = Colors.DarkRed;
//paragraph.Format.Distancne = "3cm";
Paragraph renderDate = section.AddParagraph();
renderDate = section.AddParagraph("Work Order Generated: ");
renderDate.AddDateField();
return document;
}
The line Paragraph bodyParagraph = section.AddParagraph(); adds an empty paragraph. I assume that is the extra space on the first page.
Same issue with renderDate in the following code block.
Just remove the calls section.AddParagraph() to remove the empty paragraphs if you don't want them.
MigraDoc is much like Word and understanding sections, paragraphs, &c. in Word will also help you with MigraDoc. That knowledge along with the samples and IntelliSense should get you going.
You can use MigraDoc to create an RTF file, open the RTF in Word, and click the pilcrow to show formatting characters in Word.

Copy/paste data validation in Google Spreadsheets

I feel a bit silly not being able to figure this out. So this is the data validation I have set up:
Cell Range: Journal!J2
Criteria: List from a range - Journal!W2:X2
Cell Range: Journal!M2
Criteria: List from a range - Journal!Y2:AA2
This is great in my first row. I create another row and I'd like it to update all of the '2' to '3'. The cell range updates correctly, but the criteria does not, and I can't figure out an easy solution other than going in and updating it manually.
I've tried copy/paste as well as paste special -> data validation.
I know something like $Y$2 would fix the row/col but that's the opposite of what I want. I guess I'm wanting to maintain the relative formula vs it being an absolute formula?
Indeed, the "list from a range" type of validation treats the reference to the list as absolute rather than relative. I know two workarounds:
Custom formula
Validation based on the custom formula
=not(isna(match(J2, W2:X2, 0)))
is equivalent to "value must be from the range W2:X2", and it will be copied down correctly, the reference to W2:X2 being relative.
Drawback: you don't get an in-cell dropdown list with custom formula validation.
Script
One can use an Apps Script to manage data validation rules. The following script sets data validation rules in each cell of the range J2:J100, where the value is required to be from W:X of the same row.
function validate() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("J2:J100");
var valuesColumn = 23; // begins in W
var valuesLength = 2; // has length 2, so W:X
var firstRow = range.getRow();
for (var i = 0; i < range.getHeight(); i++) {
var rule = SpreadsheetApp.newDataValidation()
.requireValueInRange(sheet.getRange(firstRow + i, valuesColumn, 1, valuesLength), true)
.setAllowInvalid(false)
.build();
range.offset(i, 0, 1, 1).setDataValidation(rule);
}
}

Using Javascript for Automation to copy cells in Numbers

I want to use JXA to automate some updating of Numbers spreadsheets. For example, copying a range of cells from one spreadsheet to another one with a different structure.
At this point, I'm just testing a simple program to set or read the value of a cell and I can't get this to work.
When I try to set a value I get "Error -1700: Can't convert types." and when I try to read a value I get back a [object ObjectSpecifier] rather than a text or number value.
Here's an example of the code:
Numbers = Application('Numbers')
Numbers.activate()
delay(1)
doc = Numbers.open(Path('/Users/username/Desktop/Test.numbers'))
currentSheet = doc.Sheets[0]
currentTable = currentSheet.Tables[0]
console.log(currentTable['name'])
console.log(currentTable.cell[1][1])
currentTable.cell[1][1].set(77)
When I run this, I get and output of [object ObjectSpecifier] for the two console.logs and then an error -1700: Can't convert types when it tries to set a cell.
I've tried several other variations of accessing or setting properties but can't get it to work.
Thanks in advance,
Dave
Here is a script that sets and gets a cell's value and then sets a different cell's value in the same table:
// Open Numbers document (no activate or delay is needed)
var Numbers = Application("Numbers")
var path = Path("/path/to/spreadsheet.numbers")
var doc = Numbers.open(path)
// Access the first table of the first sheet of the document
// Note:
// .sheets and .tables (lowercase plural) are used when accessing elements
// .Sheet and .Table (capitalized singular) are used when creating new elements
var sheet = doc.sheets[0]
var table = sheet.tables[0]
// Access the cell named "A1"
var cell = table.cells["A1"]
// Set the cell's value
cell.value = 20
// Get the cell's value
var cellValue = cell.value()
// Set that value in a different cell
table.cells["B2"].value = cellValue
Check out the Numbers scripting dictionary (with JavaScript selected as the language) to see classes and their properties and elements. The elements section will show you the names of elements (e.g. the Document class contains sheets, the Sheet class contains tables, and so on). To open the scripting dictionary, in Script Editor's menu bar, choose Window > Library, and then select Numbers in the library window.
In regards to the logging you were seeing - I recommend using a function similar to this:
function prettyLog(object) {
console.log(Automation.getDisplayString(object))
}
Automation.getDisplayString gives you a "pretty print" version of any object you pass to it. You can then use that for better diagnostic logging.

Find&Replace script in Google Docs SpreadSheets

I have google spreadsheet with direct links to images (jpg and png):
https://docs.google.com/spreadsheet/ccc?key=0AoPGWppcjtzhdDh6MW1QNVJhSHlwVTlfRnRtd0pvNGc&usp=sharing
I want to increase rows heights starting from "2nd row" to 100px and render images there.
It's possible to do via Find&Replace:
Find jpg and Replace to jpg", 1)
Find http://img and Replace to =image("http://img)
Select rows and Scale them
and the same for png image-urls.
Watch this screencast http://www.screenr.com/S0RH
Is it possible to automate it via script? I think - YES! It have to be pretty simple but I googled a lot but haven't found the solution. I can't do it myself as don't know coding. Will anyone help and make this script?
A function to do what you ask is simple, if you have a basic understanding of the language (Javascript), know how to use the development environment, and read the API documentation.
For example, see this script. It's been added to your shared spreadsheet, so you can also view it (and run it) in the script editor there.
/**
* Scan column A, looking for images that have been inserted using
* =image() function. For any row with an image, set the row height
* to 100 pixels.
*/
function resizeImageRows() {
var sheet = SpreadsheetApp.getActiveSheet(); // Get a handle on the sheet
var HEADERS = 1; // Number of header rows at top
var firstRow = HEADERS + 1; // First row with data
var lastRow = sheet.getLastRow(); // Last row with data
var imageRange = sheet.getRange(1, 1, lastRow, 1); // Column A
// Get all formulas from Column A, without Headers
var formulas = imageRange.getFormulas().slice(HEADERS);
// Look for image() formulas, and set the row height.
for (var i = 0; i< formulas.length; i++) {
if (formulas[i][0].indexOf('image') !== -1) {
sheet.setRowHeight(i+firstRow, 100); // Set height to 100 pixels
}
}
}
You can absolutely do this with the find and replace function under the edit menu, just make sure you click "search in formulas" and it will find and replace in the formula.

Resources