Jscript ReadLine() related - javascript

Can any one please tell me that we use ReadLine() to read a particular line from a file (.txt). Now I want to read the total content of the file (not only the first line). For that what method I need to use. I googled a lot but I cant get the solution.
My Code is given below:
var ForReading = 1;
var TristateUseDefault = -2;
var fso = new ActiveXObject("Scripting.FileSystemObject");
var newFile = fso.OpenTextFile(sFileName, ForReading, true, TristateUseDefault);
var importTXT = newFile.ReadLine();
This is returning the first line of the .txt file by importTXT variable. Now I want to get the total file content in importTXT.
Any suggestion will be very much helpful for me.

You use the ReadAll method:
var importTXT = newFile.ReadAll();
(Don't forget to close the stream when you are done with it.)

Here: ReadAll (msdn)
I found the example given very poor - for example it did not CLOSE the file, so I added this to the msdn page:
function ReadAllTextFile(filename)
{
var ForReading = 1;
var fso = new ActiveXObject("Scripting.FileSystemObject");
// Open the file for input.
var f = fso.OpenTextFile(filename, ForReading);
// Read from the file.
var text = (f.AtEndOfStream)?"":f.ReadAll(); // this is where it is read
f.Close();
return text;
}
var importTXT = ReadAllTextFile(sFileName);

Related

reading data line by line from txt file using javascript Rhino

I have created a txt file using python code which contents like as shown in image:
I am writing a javascript code in a Nomagic Cameo system modeler . I am reading the file but I want to read all the given lines one by one into a variable and then use this variable to open these csv files in loop and do some processing.
So far I have written the following code but I am not sure how to correct it as per my requirement.
importPackage(org.w3c.dom);
importClass(java.io.File);
importClass(java.util.Scanner);
importClass (java.util.Scanner);
//importClass(java.io.BufferedReader);
importClass(javax.xml.parsers.DocumentBuilderFactory);
importClass(javax.xml.transform.OutputKeys);
importPackage(java.sql);
importClass(com.nomagic.magicdraw.automaton.AutomatonMacroAPI);
importClass(com.nomagic.magicdraw.openapi.uml.SessionManager);
java.lang.Class.forName("com.mysql.jdbc.Driver");
var conn = DriverManager.getConnection("jdbc:mysql://"+HostName+":"+Port,Username,Password);
var stat = conn.createStatement();
var resultSet = null;
var logger = com.nomagic.magicdraw.core.Application.getInstance().getGUILog();
var fileR = new File(FilePath);
//var sc = new Scanner(fileR);
var scanner = new Scanner(new File(fileR));
while (scanner.hasNextLine()) {
var line = scanner.nextLine();
print(line);
// process the line
}

File is not defined in JavaScript

I'm writing a code in JavaScript the opens a file then read it:
var txtFile = "/dataset/happy.txt";
var fileH = new File(txtFile);
var fileS = new File("/dataset/sad.txt");
var fileA = new File("/dataset/angry.txt");
var fileSC = new File("/dataset/scared.txt");
var fileSU = new File("/dataset/suprised.txt");
fileH.open("r"); // open file with read access
But I get (ReferenceError: File is not defined) error
What's the problem?
Thank you :)

Save Excel file via web page using java script

var myApp = new ActiveXObject("Excel.Application");
myApp.visible = true;
var myWorkbook;
var xlCellTypeLastCell = 11;
myObject = new ActiveXObject("Scripting.FileSystemObject");
if(myObject.FileExists("xyz.xlsx")){
myWorkbook = myApp.Workbooks.Open("xyz.xlsx");
}
else{
myWorkbook=myApp.Workbooks.Add();
}
var myWorksheet = myWorkbook.Worksheets(1);
myWorksheet.Activate;
objRange = myWorksheet.UsedRange;
objRange.SpecialCells(xlCellTypeLastCell).Activate ;
newRow = myApp.ActiveCell.Row + 1;
strNewCell = "A" + newRow;
myApp.Range(strNewCell).Activate;
myWorksheet.Cells(newRow,1).value=document.getElementById("table1").rows[1].cells.item(0).innerHTML; myWorksheet.Cells(newRow,2).value=document.getElementById("table1").rows[1].cells.item(4).innerHTML;
myWorksheet.Cells(newRow,3).value=document.getElementById("table1").rows[1].cells.item(5).innerHTML;
myWorkbook.SaveAs("xyz.xlsx");
myApp.Quit();
The above code saves the excel for the first time, in the second loop it pops the window asking to save manually, i am trying to save the file automatically without manual intervention.
You need to switch off display alerts temporarily. This is in VBA. Change it to use myApp
Application.DisplayAlerts = False
' save here
Application.DisplayAlerts = True
The DisplayAlerts documentation is short and worth reading: https://msdn.microsoft.com/en-us/library/office/ff839782.aspx

document generation only works the first time

I'm using openxml in my HTML5 mobile app to generate word documents on the mobile device.
In general openxml works fine and straight forward, but I'm struggling with an annyoing problem.
The document generation only works the first time after I've started the app. This time I can open and view the document. Restart the app means:
- Redeploy from development machine
- Removing the app from the task pane (pushing aside; I assume the app is removed then?)
The second time I get the message the document is corrupted and I'm unable to view the file
UPDATE:
I can't reproduce this behaviour when I'm running the app connected to the remote debugger without having a breakpoint set. Doing it this way I always get a working document.
I doesn't make a difference wether I do any changes on the document or not. Simply open and saving reproduce this error.
After doing some research I've found that structure of the docx.zip file of the working and the corrupt file is the same. They also have the same file length. But in the corrupt docx there are some files I've found some files having a wrong/invalid CRC. See here an example when trying to get a corrupt file out of the zip. Other files are working as expected.
The properties for this file are->
(CRC in a working version is: 44D3906C)
Code for processing the doc-template:
/*
* Process the template
*/
function processTemplate(doc64, callback)
{
"use strict";
console.log("PROCESS TEMPLATE");
var XAttribute = Ltxml.XAttribute;
var XCData = Ltxml.XCData;
var XComment = Ltxml.XComment;
var XContainer = Ltxml.XContainer;
var XDeclaration = Ltxml.XDeclaration;
var XDocument = Ltxml.XDocument;
var XElement = Ltxml.XElement;
var XName = Ltxml.XName;
var XNamespace = Ltxml.XNamespace;
var XNode = Ltxml.XNode;
var XObject = Ltxml.XObject;
var XProcessingInstruction = Ltxml.XProcessingInstruction;
var XText = Ltxml.XText;
var XEntity = Ltxml.XEntity;
var cast = Ltxml.cast;
var castInt = Ltxml.castInt;
var W = openXml.W;
var NN = openXml.NoNamespace;
var wNs = openXml.wNs;
var doc = new openXml.OpenXmlPackage(doc64);
// add a paragraph to the beginning of the document.
var body = doc.mainDocumentPart().getXDocument().root.element(W.body);
var tpl_row = ((doc.mainDocumentPart().getXDocument().descendants(W.tbl)).elementAt(1).descendants(W.tr)).elementAt(2);
var newrow = new XElement(tpl_row);
doc.mainDocumentPart().getXDocument().descendants(W.tbl).elementAt(1).add(newrow);
// callback(doc);
var mod_file = null;
var newfile;
var path;
if (doc != null && doc != undefined ) {
mod_file = doc.saveToBlob();
// Start writing document
path = "Templates";
newfile = "Templates/Bau.docx";
console.log("WRITE TEMPLATE DOCUMENT");
fs.root.getFile("Templates/" + "MyGenerated.docx", {create: true, exclusive: false},
function(fileEntry)
{
fileEntry.createWriter(
function(fileWriter)
{
fileWriter.onwriteend = function(e) {
console.log("TEMPLATE DOCUMENT WRITTEN:"+e.target.length);
};
fileWriter.onerror = function(e) {
console.log("ERROR writing DOCUMENT:" + e.code + ";" + e.message);
};
var blobreader = new FileReader();
blobreader.onloadend = function()
{
fileWriter.write(blobreader.result); // reader.result contains the contents of blob as a typed array
};
blobreader.readAsArrayBuffer(mod_file);
},
null);
}, null);
};
Any ideas what I'm doing wrong?
Thanks for posting about the error. There were some issues with jszip.js that I encountered when I was developing the Open XML SDK for JavaScript.
At the following link, there is a sample javascript app that demonstrates generating a document.
Open XML SDK for JavaScript Demo
In that app you can save multiple DOCXs, one after another, and they are not corrupted.
In order to work on this issue, I need to be able to re-produce locally. Maybe you can take that little working web app and replace parts with your parts until it is generating invalid files?
Cheers, Eric
P.S. I am traveling and have intermittent access to internet. If you can continue the thread on OpenXmlDeveloper.org, then it will help me to answer quicker. :-)
What made it work for me, was changing the way of adding images (Parts) to the document. I was using the type "binary" for adding images to document. I changed this to "base64"
So I changed the source from:
mydoc.addPart( "/word/"+reltarget, openXml.contentTypes.png, "binary", fotodata ); // add Image Part to doc
to:
mydoc.addPart( "/word/"+reltarget, openXml.contentTypes.png, "base64", window.btoa(fotodata) ); // add Image Part to doc

translating from javascript to jquery

i'm trying to translate code from javascript to jquery and i need a little help
so , i have this code:
$('input[type=file]#upload_input_general_uploader').change(function(e){
var browserName=navigator.appName;
if (browserName=="Microsoft Internet Explorer"){
var myFSO = new ActiveXObject("Scripting.FileSystemObject");
var filepath = document.upload.file.value;
var thefile = myFSO.getFile(filepath);
var size = thefile.size;
}
}
and i tried to translate it to jquery , that's what i got but that seems to not work
$('input[type=file]#upload_input_general_uploader').change(function(e){
$browserName=navigator.appName;
if ($browserName=="Microsoft Internet Explorer"){
$myFSO = new ActiveXObject("Scripting.FileSystemObject");
$filepath = document.$(this).val();
$thefile = $myFSO.getFile($filepath);
$filesize = thefile.size;
}
}
e.g for those who asked why - i need this code to be with jquery to be dynamic , i need to work with $(this) because this function get active for few inputs.
so , what's the problem here?
Start with this line:
document.$(this).val();
$ is not a property of document, so you'll get an error like TypeError: undefined_method when that line is executed.
How about:
$('input[type=file]#upload_input_general_uploader').change(function(e){
if ($.browser.msie){
var myFSO = new ActiveXObject("Scripting.FileSystemObject");
var filepath = $(this).val();
var thefile = myFSO.getFile(filepath);
var size = thefile.size;
}
}
You don't really need to translate it if you don't need to. It's all javascript.

Categories

Resources