embed JS code in a batch file - javascript

i have a batch file preparing word files by renaming and relocating them.
so that i make pdf for this files using a javascript code i ve found in this website. i call it as follows;
for %%g in ("test\*.doc") do (cscript.exe //nologo "SAVEASPDF.js" "%%~fg")
this JavaScript code is in another file as saveaspdf.js to make PDF.
can i embed a JS code inside the batch file (e.g. as a :FUNCTION) to keep all the code in a single file only?
here is the JS i m trying to embed, i found it here in this website.
var fso = new ActiveXObject("Scripting.FileSystemObject");
var docPath = WScript.Arguments(0);
docPath = fso.GetAbsolutePathName(docPath);
var pdfPath = docPath.replace(/\.doc[^.]*$/, ".pdf");
var objWord = null;
try
{
WScript.Echo("Saving '" + docPath + "' as '" + pdfPath + "'...");
objWord = new ActiveXObject("Word.Application");
objWord.Visible = false;
var objDoc = objWord.Documents.Open(docPath);
var wdFormatPdf = 17;
objDoc.SaveAs(pdfPath, wdFormatPdf);
objDoc.Close();
WScript.Echo("Done.");
}
finally
{
if (objWord != null)
{
objWord.Quit();
}
}

There are many methods posted for embedding and executing JScript within a batch script. Here are a few:
https://stackoverflow.com/a/15169687/1012053
This is my favorite, and the one I will use below
https://stackoverflow.com/a/4999378/1012053
I don't like this method because it defines an extra (unwanted) environment variable.
https://stackoverflow.com/a/15176096/1012053 (before the EDIT)
Another excellent choice.
https://stackoverflow.com/a/9074483/1012053 (The final UPDATE 2014-04-27 at the bottom)
This WSF technique is not quite as convenient, but it is powerful in that you can embed and execute any number of independent JScript and/or VBS jobs within a single batch script.
So here is how you could use option 1. to combine the two scripts into a single file:
#if (#X)==(#Y) #end /* Harmless hybrid line that begins a JScript comment
:: ******* Begin batch code *********
#echo off
for %%g in ("test\*.doc") do cscript //E:JScript //nologo "%~f0" "%%~fg"
exit /b
********* Begin JScript code **********/
var fso = new ActiveXObject("Scripting.FileSystemObject");
var docPath = WScript.Arguments(0);
docPath = fso.GetAbsolutePathName(docPath);
var pdfPath = docPath.replace(/\.doc[^.]*$/, ".pdf");
var objWord = null;
try
{
WScript.Echo("Saving '" + docPath + "' as '" + pdfPath + "'...");
objWord = new ActiveXObject("Word.Application");
objWord.Visible = false;
var objDoc = objWord.Documents.Open(docPath);
var wdFormatPdf = 17;
objDoc.SaveAs(pdfPath, wdFormatPdf);
objDoc.Close();
WScript.Echo("Done.");
}
finally
{
if (objWord != null)
{
objWord.Quit();
}
}

Try this:
ECHO 'YOUR_JS_CODE' | node
http://www.robvanderwoude.com/redirection.php

If you look at the documentation for cscript.exe it seems that it wants to read the script from a file so I believe you are out of luck here.
Running it via nodejs as lleaff suggested might have worked if your script was compatible with nodejs but since you are using ActiveXObject to automate Word it wouldn't execute.

Related

How do I pause/resume in Photoshop with a JS code script?

I'm currently trying to automate a certain task in Photoshop (the latest version) with a JS code script. The each process is that 1. load an image -> 2. do something -> 3. implement an action -> 4. save the image -> 5. repeat this. Pretty simple, right? Cut to the chase, it's my code snippet below.
var opts;
opts = new ExportOptionsSaveForWeb();
opts.format = SaveDocumentType.PNG;
opts.PNG8 = false;
for(var i = 1; i < 6; i ++){
var filename = '/Users/abcde/Desktop/ap/' + i.toString() + '.PNG';
var file = File(filename);
var document = app.open(file);
// do something in Photoshop manually !!
app.doAction('bbbb', 'aaaa');
var savename = '/Users/abcde/Desktop/ap_save/' + i.toString() + '.PNG';
var savefile = new File(savename)
app.activeDocument.exportDocument(savefile, ExportType.SAVEFORWEB, opts);
}
So, my question is how do I pause the program when it loaded an image and do something, and get an input key like 'w' and resume the entire process again?
With my question, the code snippet is going to be like below
var opts;
opts = new ExportOptionsSaveForWeb();
opts.format = SaveDocumentType.PNG;
opts.PNG8 = false;
for(var i = 1; i < 6; i ++){
var filename = '/Users/abcde/Desktop/ap/' + i.toString() + '.PNG';
var file = File(filename);
var document = app.open(file);
// pause this code script (which type of code can I put in here?)
// do something(I'm going to do something manually in the Photoshpp when this script's been paused, this part doesn't has to be written in code)
// resume when it gets a key input(I want to type 'w' when that something task has been done, again.. which type of code function can I put?)
app.doAction('bbbb', 'aaaa');
var savename = '/Users/abcde/Desktop/ap_save/' + i.toString() + '.PNG';
var savefile = new File(savename)
app.activeDocument.exportDocument(savefile, ExportType.SAVEFORWEB, opts);
}
Please help me.
If either it's impossible to build or I need to clarify this question more let me know too.
Thanks.
I would restructure the script to run the export first (in case there is already a file open and then open the next document. Then, when the script is finished, the user can do something with the open document in Photoshop. Once they are done with that, they run the script again.
This needs a bit of extra checking if there is a document already open and also if there are any files left to handle. But it would basically look like this:
// set export options
var opts;
opts = new ExportOptionsSaveForWeb();
opts.format = SaveDocumentType.PNG;
opts.PNG8 = false;
// find out, which destination files already exists, to know which new one to open
// increase i until it finds an index that does not exist yet
var i = 1;
while( File('/Users/abcde/Desktop/ap_save/' + i.toString() + '.PNG').exists ) {
i++;
}
// if a document is already open, run the action on it and export it
if(app.documents.length) {
app.doAction('bbbb', 'aaaa');
var savename = '/Users/abcde/Desktop/ap_save/' + i.toString() + '.PNG';
var savefile = new File(savename)
app.activeDocument.exportDocument(savefile, ExportType.SAVEFORWEB, opts);
app.activeDocument.close();
// increase i once more to jump to the next file
i++;
}
// try to open a new source file, if there is one left
var srcFile = File('/Users/abcde/Desktop/ap/' + i.toString() + '.PNG)'
if(!srcFile.exists) {
// no further source file does exist, exit the script
exit();
}
var document = app.open(file);
// script ends here and the user can do stuff
// and then use a hot key to run the script again

Is there a way I can automate the creation of .json files used for language translations?

I have files such as this that have translation keys and values:
locale-en.json
{
"CHANGE_PASSWORD": "Change Password",
"CONFIRM_PASSWORD": "Confirm Password",
"NEW_PASSWORD": "New Password"
}
locale-jp.json
{
"CHANGE_PASSWORD": "パスワードを変更します",
"CONFIRM_PASSWORD": "パスワードを認証します",
"NEW_PASSWORD": "新しいパスワード"
}
When I add a new translation key to the JSON file containing the English translations for example, I must remember to add that key and the associated translation to all the other JSON files. All the JSON files are also edited separately. The process is laborious and error prone.
Has anyone found a way to reduce the errors and to automate the process.
Ideally I would like to be able to run a script from Windows PowerShell that would change the files to this if an additional key was added to locale-en.json :
locale-en.json
{
"CHANGE_PASSWORD": "Change Password",
"CONFIRM_PASSWORD": "Confirm Password",
"NEW_PASSWORD": "New Password",
"NEW_KEY": "New Key"
}
locale-jp.json
{
"CHANGE_PASSWORD": "パスワードを変更します",
"CONFIRM_PASSWORD": "パスワードを認証します",
"NEW_PASSWORD": "新しいパスワード",
>>>"NEW_KEY": "New Key"
}
You could write something like this in powershell:
$masterFile = "locale-en.json"
function Get-LocaleMap($file){
$map = #{}
$localeJson = ConvertFrom-Json (gc $file -Raw)
$localeJson | gm -MemberType NoteProperty | % {
$map.Add($_.Name, ($localeJson | select -ExpandProperty $_.Name))
}
return $map
}
$masterLocale = Get-LocaleMap $masterFile
ls | ? { $_.Name -like "locale-*.json" -and $_.Name -ne $masterFile } | % {
$locale = Get-LocaleMap $_.FullName
$masterLocale.GetEnumerator() | % {
if(!$locale.ContainsKey($_.Key)){
$locale.Add($_.Key, $_.Value)
}
}
ConvertTo-Json $locale | Out-File -FilePath $_.FullName -Force -Encoding utf8
}
It created a dictionary from your English json file. Then it looks up all other locale files and checks them for keys which are present in the English file but missing from them. Then it adds the missing keys and values and saves the locale files in Unicode.
Let me show you how you can do the same with old school Windows Scripting since you seem to prefer JavaScript:
var masterFile = "locale-en.json"
var fso = new ActiveXObject("Scripting.FileSystemObject");
var scriptPath = fso.GetParentFolderName(WScript.ScriptFullName);
var charSet = 'utf-8';
var f = fso.GetFolder(scriptPath);
var fc = new Enumerator(f.files);
function getLocaleMap(fileName){
var path = scriptPath + '\\' + fileName;
var stream = new ActiveXObject("ADODB.Stream"); // you cannot use fso for utf-8
try{
stream.CharSet = charSet;
stream.Open();
stream.LoadFromFile(path);
var text = stream.ReadText();
var json = {};
eval('json = ' + text); // JSON.parse is not available in all versions
return json;
}
finally{
stream.Close();
}
}
function saveAsUtf8(fileName, text){
var path = scriptPath + '\\' + fileName;
var stream = new ActiveXObject("ADODB.Stream");
try{
stream.CharSet = charSet;
stream.Open();
stream.Position = 0;
stream.WriteText(text);
stream.SaveToFile(path, 2); // overwrite
}
finally{
stream.Close();
}
}
var locales = [];
var masterMap = getLocaleMap(masterFile);
for (; !fc.atEnd(); fc.moveNext())
{
var file = fc.item();
var extension = file.Name.split('.').pop();
if(extension != "json" || file.Name == masterFile){
continue;
}
var map = getLocaleMap(file.Name);
var newLocaleText = '{\r\n';
var i = 0;
for(var name in masterMap){
var value = '';
if(map[name]){
value = map[name];
}
else{
value = masterMap[name];
}
if(i > 0){
newLocaleText += ",\r\n";
}
newLocaleText += "\t'" + name + "': '" + value + "'";
i++;
}
newLocaleText += '\r\n}'
saveAsUtf8(file.Name, newLocaleText);
}
You can run the javascript from command line like this:
Cscript.exe "C:\yourscript.js"
I hope it helps.
Is there a way I can automate the creation of .json files used for language translations?
YES, executing automatic tasks is exactly what automation tools like Grunt and Gulp where designed to do.
As you said, doing things manually is laborious and error prone, so Grunt/Gulp are the way to go.
With a simple Grunt/Gulp config, all the relevant .json files can be watched simultaneously: any key added to any of them will be instantly detected, and order the execution of the custom script of your choice.
HOW GRUNT/GULP CAN DO IT:
Grunt/Gulp will constantly watch all the relevant JSON files;
When a change is detected in a watched file, a custom script is run;
The custom script will read the changed file and retrieve the new key(s) and value(s);
The custom script will then be write to all the other relevant JSON files.
CONFIGURING GRUNT
To detect file changes automatically and execute myCustomScript, just use grunt-contrib-watch like so:
watch: {
scripts: {
files: ['**/*.locale.json'],
tasks: ['myCustomScript'],
},
}
CUSTOM SCRIPT TO ADD THE NEW KEY(S) TO THE RELEVANT .JSON FILES:
grunt.event.on('watch', function(action, filepath) {
// filepath is the path to the file where change is detected
grunt.config.set('filepath', grunt.config.escape(filepath));
});
var myCustomScript=function(changedFile,keyFile){
var project = grunt.file.readJSON(changedFile);
//will store the file where changes were detected as a json object
var keys=grunt.file.readJSON(keyFile);
//will store keyFile as a json object
//walk changedFile keys, and check is keys are in keyFile
for (var key in project) {
if (project.hasOwnProperty(key)) {
if(!keys.hasOwnProperty(key)){
//a new key was detected
newKeyArray.push(key);
}
}
}
//should update all the other relevant JSON files with `grunt.file.write`, and add all the keys in newKeyArray:
var filesToChangeArray=grunt.file.match('**/*.locale.json');
//returns an array that contains all filepaths where change is desired
filesToChangeArray.forEach(function(path){
//walk newKeyArray to set addedContent string
newKeyArray.forEach(function(key){
addedContent+='"'+key+'":"to be set",';
//this will write all the new keys, with a value of "to be set", to the addedContent string
}
grunt.file.write(path,addedContent);
});
}
Ideally I would like to be able to run a script from Windows PowerShell
Even though Grunt/Gulp are often used to execute custom files written in javaScript/nodejs, they are well able to order the execution of scripts written in other languages.
To execute a PowerShell script, you could use a Grunt plugin called grunt-shell, like so:
grunt.initConfig({
shell: {
ps: {
options: {
stdout: true
},
command: 'powershell myScript.ps1'
}
}
});
as detailed in this SO post.
So if PowerShell is your thing, you could have the best of both worlds:
Easy detection with Grunt/Gulp watch;
PowerShell script execution when change is detected.
However, you might as easily use Grunt/Gulp only for this: as Grunt/Gulp is already taking care of the detection in the background, all you need to do is have it run a custom script that reads your new keys (grunt.file.readJSON) and copies them (grunt.file.write) to the relevant files.
Automated the process using a javascript solution with nodejs via command line.
$ node localeUpdater.js
This will watch your default locale (locale-en.json) with any revisions made and update your whole locale file list as necessary.
create the necessary locale file list if not present then initialized it with default locale data
add new keys based on default locale
remove missing keys based on default locale
localeUpdater.js
var fs = require("fs");
var localeFileDefault = "locale-en.json";
var localeFileList = ["locale-jp.json", "locale-ph.json"];
fs.watchFile(localeFileDefault, function() {
var localeDefault = readFile(localeFileDefault);
var localeCurrent = null;
var fileNameCurrent = null;
for (var i in localeFileList) {
fileNameCurrent = localeFileList[i];
console.log("Adding new keys from default locale to file " + fileNameCurrent);
localeCurrent = readFile(fileNameCurrent);
for (var key in localeDefault) {
if (!localeCurrent[key]) {
console.log(key + " key added.");
localeCurrent[key] = localeDefault[key];
}
}
console.log("Removing keys not on default locale to file " + fileNameCurrent);
for (var key in localeCurrent) {
if (!localeDefault[key]) {
console.log(key + " key removed.");
delete localeCurrent[key];
}
}
writeFile(fileNameCurrent, JSON.stringify(localeCurrent));
console.log("File " + fileNameCurrent + " updated.");
}
});
function readFile(fileName) {
var result = null;
if (fs.existsSync(fileName)) {
result = fs.readFileSync(fileName, "utf8");
result = result ? JSON.parse(result) : {};
} else {
writeFile(fileName, "{}");
result = {};
}
return result;
}
function writeFile(fileName, content) {
fs.writeFileSync(fileName, content, "utf8");
}
There are multiple safeguards you should put in place.
First off your translation function should have some safeguards. Something like:
function gettext(text) {
if (manifest[text]) {
return text;
}
return text;
}
I'm not sure how you register new strings, but we regex our code base for things like gettext('...') and then we compile a list of translations that way. A couple times a day we push that to a 3rd party translation company, which notices new strings. They populate new things and we pull content back. The "pull" involves a compilation to the different language files. The translation file compilation always falls back to english. In other words we download a file from the 3rd party and do something like:
_.map(strings, function(string) {
return localeManifest[locale][text] || localeManifest['en_US'][text];
}
This ensures that even if the manifest for the locale doesn't contain the translation yet we still populate it with the English US version.

Node.js transform stream - Find the source name in _transform function

I have installed node.js on Windows 7, and I am using basic example of Transform stream to unzip log files in a directory and read log files asynchronously using _transform function.
This process is very fast, but now I have a problem:
I need to map source filename to the filtered lines. I tried appending filename to the filtered line in on.readable event but its incorrect since data comes out asynchronously from multiple files so maps everything to a single filename. I tried some code like below but this throws error saying too many event listeners created.
source.pipe(gunzip).pipe(liner).pipe(new Upper(filename).pipe(process.stdout));
Found the solution basically need to create separate pipeline for each line, the improvised code is as below:
var gunzip = [];
var source = [];
var upper = [];
var outputfile = [];
var liner = [];
.
.
.
function file(i) {
var filename = files[i];
gunzip[i] = zlib.createGunzip();
source[i] = fs.createReadStream(__dirname + '/' + filename);
upper[i] = new Upper();
liner[i] = new LineSplitter();
outputfile[i] = fs.createWriteStream(__dirname + '/' + '_' + i + '_outputfile.txt');
source[i].pipe(gunzip[i]).pipe(liner[i]).pipe(upper[i]).pipe(outputfile[i]);
}
As further steps, looking to use async module to run these pipelines in parallel.

createFile() in google Apps Script is not functioning properly

I am trying to create a file. It works fine when I run the following code segment from the debugger in apps script. However, when I run it real time from the spreadsheet, it says I do not have permission to call createfile. Everything that is logged is identical. The issue is not I do not have authority as I am the only one in the spreadsheet and am the owner. The purpose of the CSV is to move it from my google drive into data for BigQuery
function saveAsCSV(row) { //Doc to Csv
//row = 3; //when this is uncommented and ran from the debugger, it works.
try{
var fileName= Date.now()
fileName = fileName + ".csv";
var csvFile = convertRangeToCsvFile_(fileName,row);
Logger.log(csvFile); //Both times ran on the spreadsheet and from debug equals the same.
DriveApp.createFile(fileName, csvFile);
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("New and Open").getRange("J" + row.toString()).setValue("");
loadCsv(fileName);
}
catch(e){Logger.log("B" + e.message);} //No permission to create file
}
function convertRangeToCsvFile_(csvFileName, r) {
var ws = SpreadsheetApp.getActiveSpreadsheet();
try {
//var data = ws.getValues();
var csvFile = undefined;
var csv = "";
var row = r;
var datArray = Create2DArray(1,19);
datArray[0][0] = ws.getRange("A" + row.toString()).getValue().toString().toUpperCase();
datArray[0][1] = ws.getRange("B"+row.toString()).getValue().toString().toUpperCase();
datArray[0][2] = ws.getRange("C"+row.toString()).getValue().toString().toUpperCase();
datArray[0][3] = ws.getRange("D"+row.toString()).getValue().toString().toUpperCase();
datArray[0][4] = ws.getRange("E"+row.toString()).getValue().toString().toUpperCase();
datArray[0][5] = ws.getRange("F"+row.toString()).getValue().toString().toUpperCase();
datArray[0][6] = ws.getRange("G"+row.toString()).getValue().toString().toUpperCase();
datArray[0][7] = ws.getRange("H"+row.toString()).getValue().toString().toUpperCase();
datArray[0][8] = ws.getRange("I"+row.toString()).getValue().toString().toUpperCase();
datArray[0][9] = new Date(ws.getRange("K"+row.toString()).getValue().toString()).getHours();
datArray[0][10] = new Date(ws.getRange("K"+row.toString()).getValue().toString()).getMinutes();
datArray[0][11] = new Date(ws.getRange("L"+row.toString()).getValue().toString()).getHours();
datArray[0][12] = new Date(ws.getRange("L"+row.toString()).getValue().toString()).getMinutes();
datArray[0][13] = new Date(ws.getRange("M"+row.toString()).getValue().toString()).getHours();
datArray[0][14] = new Date(ws.getRange("M"+row.toString()).getValue().toString()).getMinutes();
datArray[0][15] = new Date(ws.getRange("N"+row.toString()).getValue().toString()).getTime();
datArray[0][16] = new Date(ws.getRange("N"+row.toString()).getValue().toString()).getFullYear();
datArray[0][17] = new Date(ws.getRange("N"+row.toString()).getValue().toString()).getMonth();
datArray[0][18] = new Date(ws.getRange("N"+row.toString()).getValue().toString()).getDate();
for(var i = 0; i < 19; i++){
if(datArray[0][i] == ""){if(i > 9){datArray[0][i] = 0;} else{datArray[0][i] = "nil";} }
if(i < 18){csv += '"' + datArray[0][i] + '"' + ",";}
else{ csv += '"' + datArray[0][i] + '"'; }
}
Logger.log("A " + csv);
Logger.log(csv + "\n" + datArray[0].join(","));
csvFile = csv;
return csvFile;
}
catch(err) {
Logger.log("C" + err);
Browser.msgBox(err);
}
}
You mention in your comment on my answer that you are using onEdit to trigger the script. Since this is a Simple Trigger, your current approach will not work. When you use simple triggers to run an Apps Script, it runs in a sandbox with reduced permissions.
See: https://developers.google.com/apps-script/guides/triggers/#restrictions
The best I can recommend is create a custom menu option with a UI popup asking for the row number to export. If the code is triggered from a menu by the user, it runs with full permission to access that users account.
Depending on your use-case, a scheduled trigger might work too. It could run every 10 minutes or every Hour and export any changes to the spreadsheet. In this case the Apps Script runs as you, with permission to access your account, and the resulting CSV would be created on your drive.
Details on how to create a custom menu: https://developers.google.com/apps-script/guides/triggers/#onopen
Details on how to create a form for the user: https://developers.google.com/apps-script/guides/ui-service
Details on time driven triggers: https://developers.google.com/apps-script/guides/triggers/installable#time-driven_triggers

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

Categories

Resources