Are multiple async file appends on the same file safe? - node.js

Looking at the pseudo-code below, is it possible for the writes to the file to become mangled?
for(var i=0;i<5;i++)
fs.appendFile("myfile.txt", "myline"+i+'\n', somecallback)
fs is found here
Possibility I'd expect:
myline3
myline4
myline1
myline2
myline0
But would this be possible?
mylimyline4
ne3
myline1
myline2
myline0
In which case the second append would have occurred in the middle of the first. Because if this can happen I'll have to queue the writes manually.

I wrote a programm to test that and was unable to make it mix different appends.
var fs = require('fs')
var filename = __dirname + '/file.bin'
var bytes_per_buff = parseInt(process.argv[2]) || 4096
var num_buffs = parseInt(process.argv[3]) || 256
var buffs = []
for (var i=0; i<num_buffs; i++) {
buffs[i] = new Buffer(bytes_per_buff)
for (var j=0; j<bytes_per_buff; j++) {
buffs[i][j] = i
}
}
fs.writeFile(filename, '', ()=>console.log('file created'))
for (var i=0; i<num_buffs; i++) {
(function(buff_num) { //closure to log buff_num
fs.appendFile(filename, buffs[buff_num], ()=>console.log(buff_num))
}(i))
}

Related

web console - grab all links - HELP needed

I found this useful code
var x = document.querySelectorAll("a");
var myarray = []
for (var i=0; i<x.length; i++){
var nametext = x[i].textContent;
var cleantext = nametext.replace(/\s+/g, ' ').trim();
var cleanlink = x[i].href;
myarray.push([cleantext,cleanlink]);
};
function make_table() {
var table = '<table><thead><th>Name</th><th>Links</th></thead><tbody>';
for (var i=0; i<myarray.length; i++) {
table += '<tr><td>'+ myarray[i][0] + '</td><td>'+myarray[i][1]+'</td></tr>';
};
var w = window.open("");
w.document.write(table);
}
make_table()
When you add this to the web browser console - it opens up a new webpage with all weblinks with names
however some of the names are shortned.
is there a way to edit it so it shows the entire text name for that link

Creating a attachment from Google sheet in ms Excel format via Google script

Is it possible to create an Excel format file from a Google sheet using Google script, so that it can be added as an attachment to an email?
I've got a code that takes columns with certain names (e.g. A, C, F) and turns them into a new sheet (on createCustomStatusTable() function).
https://docs.google.com/spreadsheets/d/1fZ0JMYjoIrfPIxFBVgDNU0x5X0ll201ZCU-lcaTwwcI/edit?usp=sharing
var expected = ['A','C','F'];
var newSpreadSheet = SpreadsheetApp.getActiveSheet();
var tableLastRow = newSpreadSheet.getLastRow();
var tablelastColumn = newSpreadSheet.getLastColumn();
var values = newSpreadSheet.getRange(1, 1, tableLastRow, tablelastColumn).getValues();
var rangeToCopy = [];
function in_array(value, array)
{
for(var i = 0; i < array.length; i++)
{
if(array[i] == value) return true;
}
return false;;
};
function columnsCount() {
var count = 1;
for (var i = 0; i < SpreadsheetApp.getActiveSheet().getLastColumn(); i++) {
if (in_array(values[0][i],expected))
count++;
}
return count;
};
function returnRange() {
for (var i = 1; i < SpreadsheetApp.getActiveSheet().getLastColumn()+1; i++) {
if (in_array(values[0][i-1],expected)) {
rangeToCopy.push(newSpreadSheet.getRange(1, i, newSpreadSheet.getMaxRows()));
};
};
return rangeToCopy;
};
function createCustomStatusTable() {
var targetSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Target');
for (var i = 1; i < columnsCount(); i++) {
returnRange()[i-1].copyTo(targetSheet.getRange(1,i));
};
};
Thank you in advance for any help
You can create an EXCEL type file with DriveApp:
The problem is, that the content must be a string. And I haven't tested for a way to make that work.
I know this doesn't answer your question. Hopefully someone knows for sure how to create an EXCEL file from a Google Sheet.

How can I create a new document out of a subset of another document's pages (in InDesign (CS6) using ExtendScript)?

I need to offer a feature which allows InDesign users to select a page range in an InDesign document and create a new document out of those pages. This sounds simple, but it isn't...
I have tried many different ways of doing this but they have all failed to some degree. Some methods put all pages in a single spread (which sometimes makes InDesign crash). The best I've been able to do (see code below) still has problems at the beginning and the end (see screenshots below):
The original document:
The new document:
The question: How can I create a new document out of a subset of another document's pages (in InDesign using ExtendScript) without having the problems shown in the screenshots?
note: The behavior of the script is quite different in CS5.5 and CS6. My question concerns CS6.
The second screenshot was obtained by applying the following code to the document shown in the first screenshot:
CODE
var firstPageName = { editContents: "117" }; // This page number is actually entered by the user in an integerEditbox
var lastPageName = { editContents: "136" }; // This page number is actually entered by the user in an integerEditbox
var sourceDocument = app.activeDocument;
var destDocument = app.documents.add();
destDocument.importStyles(ImportFormat.paragraphStylesFormat, new File(sourceDocument.filePath + "/" + sourceDocument.name), GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE);
destDocument.importStyles(ImportFormat.characterStylesFormat, new File(sourceDocument.filePath + "/" + sourceDocument.name), GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE);
destDocument.viewPreferences.horizontalMeasurementUnits = sourceDocument.viewPreferences.horizontalMeasurementUnits;
destDocument.viewPreferences.verticalMeasurementUnits = sourceDocument.viewPreferences.verticalMeasurementUnits;
destDocument.documentPreferences.facingPages = sourceDocument.documentPreferences.facingPages;
destDocument.documentPreferences.pageHeight = sourceDocument.documentPreferences.pageHeight;
destDocument.documentPreferences.pageWidth = sourceDocument.documentPreferences.pageWidth;
destDocument.documentPreferences.pageSize = sourceDocument.documentPreferences.pageSize;
var sourceSpreads = sourceDocument.spreads;
var nbSourceSpreads = sourceSpreads.length;
var firstPageFound = false;
var lastPageFound = false;
var i;
var newSpreadNeeded;
var currentDestSpread;
for (i = 0; !lastPageFound, i < nbSourceSpreads; ++i) {
newSpreadNeeded = true;
var sourcePages = sourceSpreads[i].pages;
var nbSourcePages = sourcePages.length;
var j;
for (j = 0; !lastPageFound, j < nbSourcePages; ++j) {
if (sourcePages[j].name === firstPageName.editContents) {
firstPageFound = true;
destDocument.documentPreferences.startPageNumber = parseInt(firstPageName.editContents); // We want to preserve page numbers
}
if (firstPageFound) {
// Copy this page over to the new document.
var firstInNewSpread = false;
if (newSpreadNeeded) {
currentDestSpread = destDocument.spreads.add();
newSpreadNeeded = false;
firstInNewSpread = true;
}
var newPage = sourcePages[j].duplicate(LocationOptions.AT_END, currentDestSpread);
var k;
for (k = 0; k < newPage.index; ++k) {
currentDestSpread.pages[k].remove();
}
}
if (sourcePages[j].name === lastPageName.editContents) {
lastPageFound = true;
}
}
}
destDocument.spreads[0].remove();
I was hacking around and came up with this little script. Although it approaches the problem from the opposite direction, it seems to work fine here. Also, I'm still running in InDesign CS5, but maybe it will work for you. Hopefully I got the gist of your question?
This will extract pages 3 through 5 into a separate document:
var doc = app.activeDocument;
var newFilePath = doc.filePath + "/subset_" + doc.name;
var newFile = File(newFilePath); // Create a new file path
doc.saveACopy(newFile); // Save a copy of the doc
var newDoc = app.open(newFile); // Open the copy
var firstPageNum = 3; // First page number in the range
var lastPageNum = 5; // Last page number in the range
var firstPage = newDoc.pages[firstPageNum-1];
var lastPage = newDoc.pages[lastPageNum-1];
// Remove all text from the last page in the range to the end of the document
var lastPageFrames = lastPage.textFrames.everyItem().getElements();
for (var i=0; i < lastPageFrames.length; i++) {
var frame = lastPageFrames[i];
var parentStory = frame.parentStory;
var lastFrameInsert = frame.insertionPoints.lastItem();
var lastStoryInsert = parentStory.insertionPoints.lastItem();
var textAfter = parentStory.insertionPoints.itemByRange(lastFrameInsert,lastStoryInsert);
textAfter.remove();
};
// Remove all text from the beginning of the document to the first page in the range
var firstPageFrames = firstPage.textFrames.everyItem().getElements();
for (var i=0; i < firstPageFrames.length; i++) {
var frame = firstPageFrames[i];
var parentStory = frame.parentStory;
var firstFrameInsert = frame.insertionPoints.firstItem();
var textBefore = parentStory.insertionPoints.itemByRange(0,firstFrameInsert.index);
textBefore.remove();
};
// Remove the pages that aren't in the range
var allPages = newDoc.pages.everyItem().getElements();
for (var i=0; i < allPages.length; i++) {
var page = allPages[i];
if (i < firstPageNum || i > lastPageNum) {
page.remove();
}
};

Union function - Passing argument in custom function - Google-spreadsheet

While trying to answer
this question, I found a strange behavior.
Here's my code :
function remove(val, array){
var res = new Array();
for(var i=0; i<array.length; i++){
if(array[i] != val){
res.push(array[i]);
}
}
return res;
}
//we assume that there is no duplicates values inside array1 and array2
function my_union(array1, array2){
var longuer;
var shorter;
var arrayRes = new Array();
if(array1.length < array2.length){
longuer = array2;
shorter = array1;
} else {
longuer = array1;
shorter = array2;
}
for(var i=0; i<longuer.length; i++){
arrayRes.push(longuer[i]);
shorter = remove(longuer[i], shorter);
}
for(var i=0; i<shorter.length; i++){
arrayRes.push(shorter[i]);
}
return arrayRes;
}
function test(){
Browser.msgBox(my_union([1,2,3], [1,2]));
}
The message box clearly says 1,2,3 but when you try to invoke this function inside a spreadsheet with the same values, it fails to delete duplicated values, why ?
**EDIT : **
Thanks to Henrique's answer , here's the code :
function remove(val, array){
var res = new Array();
for(var i=0; i<array.length; i++){
if(array[i] != val){
res.push(array[i]);
}
}
return res;
}
function matFlattener(matrix){
var array = new Array();
for(var i=0; i<matrix.length; i++){
for(var j=0; j<matrix[i].length; j++){
array.push(matrix[i][j]);
}
}
return array;
}
function my_union(matrix1, matrix2){
//assert no duplicate values in matrix1 and matrix2
var longuer;
var shorter;
var array1 = matFlattener(matrix1);
var array2 = matFlattener(matrix2);
var arrayRes = new Array();
if(array1.length < array2.length){
longuer = array2;
shorter = array1;
} else {
longuer = array1;
shorter = array2;
}
for(var i=0; i<longuer.length; i++){
arrayRes.push([longuer[i]]);
shorter = remove(longuer[i], shorter);
}
for(var i=0; i<shorter.length; i++){
arrayRes.push([shorter[i]]);
}
return arrayRes;
}
When you call the custom function from the spreadsheet and pass a multi-range parameter, this parameter will always be a matrix, regardless if you pass a single row or column.
To test the behavior like the spreadsheet does, you should change your test function like this:
function test() {
//note the "extra" brackets
Browser.msgBox(my_union([[1,2,3]],[[1,2]]); //passing "single" rows
Browser.msgBox(my_union([[1],[2],[3]],[[1],[2]]); //passing "single" columns
}
The solution is adjust your my_union formula to account for it.

Getting NaN for variables in Node.js.. Arg?

ok, I have a homework assignment where I have to read in files and calculate the distance between a bunch of numbers in the files and then print out the mean and standard deviation of each set of numbers. The end of the script, where the console.log stuff is, is giving all NaN for the variables. Can anyone help me out?
*I've omitted repeating parts of the script to make it shorter (their are more arrays than just the lHipJoint array and the calculations for them but I left them out).
var fs = require('fs');
var lHipJoint = new Array();
//open the first text file
fs.readFile('file.txt','utf8', function (err, data)
{
if (err) throw err;
//split the data into an array with each line as an element
stuff=data.split('\n');
for (var i = 0; i < stuff.length; i++)
{
//function that processes each line into an array
//with each number as an element and does the euclidean dis.
processLine(stuff[i]);
}
data.length = 0;
stuff.length = 0;
});
//do the same for the next file
fs.readFile('file2.txt','utf8', function (err, data)
{
if (err) throw err;
stuff=data.split('\n');
for (var i = 0; i < stuff.length; i++)
{
processLine(stuff[i]);
}
data.length = 0;
stuff.length = 0;
});
//and again
fs.readFile('file3.txt','utf8', function (err, data)
{
if (err) throw err;
stuff=data.split('\n');
for (var i = 0; i < stuff.length; i++)
{
processLine(stuff[i]);
}
data.length = 0;
stuff.length = 0;
});
//and again
fs.readFile('file4.txt','utf8', function (err, data)
{
if (err) throw err;
stuff=data.split('\n');
for (var i = 0; i < stuff.length; i++)
{
processLine(stuff[i]);
}
data.length = 0;
stuff.length = 0;
});
//and again
fs.readFile('file5.txt','utf8', function (err, data)
{
if (err) throw err;
stuff=data.split('\n');
for (var i = 0; i < stuff.length; i++)
{
processLine(stuff[i]);
}
data.length = 0;
stuff.length = 0;
});
//and again
fs.readFile('file6.txt','utf8', function (err, data)
{
if (err) throw err;
stuff=data.split('\n');
for (var i = 0; i < stuff.length; i++)
{
processLine(stuff[i]);
}
data.length = 0;
stuff.length = 0;
});
//function to split each line into an array with each number as an element
//then parse the number strings into floats and do the euclidean distances,
//storing the values in arrays for each bone.
function processLine(line)
{
var line1 = line
var numbers = line1.split(" ");
line1.length = 0;
for (var i = 0; i < numbers.length; i++)
{
var number = parseFloat(numbers[i]);
line1[i] = number[i];
}
lHipJoint = Math.sqrt((line1[6] - line1[9])*(line1[6] - line1[9]) + (line1[7] - line1[10])*(line1[7] - line1[10]) + (line1[8] - line1[11])*(line1[8] - line1[11]));
//reset the arrays so they can be reused
line1.length = 0;
numbers.length = 0;
number.length = 0;
}
//calculations and output for the mean and SD of each bone's distance from the root bone.
for(var i = 0; i < lHipJoint.length; i++)
{
var lHipJointTotal = lHipJointTotal + lHipJoint[i];
}
var lHipJointMean = lHipJointTotal/lHipJoint.length;
for(var i = 0; i < lHipJoint.length; i++)
{
var lHipJointSDSum = lHipJointSDSum + (lHipJoint[i] - lHipJointMean)*(lHipJoint[i] - lHipJointMean);
}
var lHipJointSD = Math.sqrt(lHipJointSDSum/lHipJoint.length);
console.log("The mean distance of the left hip joint from the root bone is " +lHipJointMean+ " and the standard deviation is " +lHipJointSD+ ".\n");
You are doing a lot of strange things here in your script i will try to
bring upp as manny as i can.
So first of all dont reset arrays.
your in a garbage collected language just reallocate new ones.
Also in the processLine function you are assigning numbers to the indexes of a string
i asume you think its an array but its not the same thing.
strings are immutable (cant be changed) in javascript.
In the aggregating for loops att the bottom of the file you are
declaring the variable in every iteration. you want to declare it before the loop like this.
var x = 0;
for(var i = 0; i < list.length; i++) {
x = x + ......
}
Your cals to read the files all do the same thing.
So you want to use the same function for that.
write it ones.
You are assigning to the lHipJoint array in the
processLine function my understanding is that you want to add
the calculated value to the array.
You can do this with the push method like this
lHipJoint.push(Math.sqr(........
Also theres a problem with using the async file reading
sins your printing the result before you even read the files.
if you want to use the async ones you need to coordinate so that.
you only print the result when you done all the file reading.
but a tips is to use the non async ones in this case.
I understand this is an assignment so you might not want to read my
attempt to correct the program beneath.
Maybe read it after you handed in yours, but im leaving it here
for the q&a reference for others reading this.
var fs = require("fs");
var filePaths = ["file.txt", "file2.txt",
"file3.txt", "file4.txt",
"file5.txt", "file6.txt"];
var lHipJoint = [];
filePaths.forEach(function(path) {
var content = fs.readFileSync(path, "utf-8");
var lines = content.split("\n");
lines.forEach(function(line) {
if(line.trim() === "") return;
var numbers = line.split("\t").map(parseFloat);
// im not touching your calculation :D
lHipJoint.push(Math.sqrt((numbers[6] - numbers[9])*(numbers[6] - numbers[9])
+ (numbers[7] - numbers[10])*(numbers[7] - numbers[10]) + (numbers[8] - numbers[11])
* (numbers[8] - numbers[11])));
});
});
var lHipJointTotal = lHipJoint.reduce(function(p, c) {
return p + c;
});
var lHipJointMean = lHipJointTotal / lHipJoint.length;
var lHipJointSDSum = lHipJoint.reduce(function(p, c) {
return p + (c - lHipJointMean) * (c - lHipJointMean);
}, 0);
var lHipJointSD = Math.sqrt(lHipJointSDSum/lHipJoint.length);
console.log("The mean distance of the left hip joint from the root bone is "
+ lHipJointMean + " and the standard deviation is " + lHipJointSD + ".\n");
there might be some error in this program i dont know how the data looks but i hope this helps
you.

Resources