using getElementById to get input from a multiline text file into id - javascript

I'm a novice at javascript and I'm trying to collect and display information from a text file for manipulation. It is for a caption that needs to be displayed when the program starts up.
So far, I can get the function to work for one line, but when I tried using it for multiple lines, it only grabs the first line of text from the text file and displays it as many times I call it.
I know I need to maybe use an array, but that's a bit more advanced than where I'm currently at. Please let me know if using an array is the most efficient or only way to do this. The sample code is below, and I'm not married to my idea. I'm just trying to figure out the best way to make it work. My group has a template maker, but I can always change they way to do this if it is better.
function write_caption_view(){
var fsow;
fsow = new ActiveXObject("Scripting.FileSystemObject");
text_write = fsow.OpenTextFile(" ", 2, true, -2);
text_write.writeline(document.getElementById("cap").value);
text_write.Close();
stat_write = fsow.OpenTextFile(" ", 2, true, -2);
stat_write.writeline("CONFIGURED");
stat_write.close();
alert("Saved Caption");
}
function read_caption_view(){
var fsor;
fsor = new ActiveXObject("Scripting.FileSystemObject");
text_read = fsor.OpenTextFile(" ", 1, false, -2);
caption_notice = text_read.readline();
text_read.Close();
document.getElementById('cap').value = caption_notice;
}
function display_caption_view(){
document.getElementById('cap').innerHTML = caption_notice;
}

There is a nice example that shows how to split a multiline string into arrays
Basically, you have to split the string by newline
var lines = $('#input').val().split('\n');
or in your case
var lines = document.getElementById("cap1").value.split('\n');
Now in lines you have an array of strings. Those can be processed further with an iterator.
Please notice a simpler way to define a multiple line string in javascript is:
var multiStr = "This is the first line \
This is the second line \
This is more...";

Thanks for the assistance. I figured out a way to do so without an array, and while not as elegant, it works and provides a version that easy to update.
function write_caption_view(){
var fsow, fsor;
fsor = new ActiveXObject("Scripting.FileSystemObject");
fsow = new ActiveXObject("Scripting.FileSystemObject");
text_write = fsow.OpenTextFile("file location", 2, true, -2);
text_write.writeline(document.getElementById("capline1").value);
text_write.writeline(document.getElementById("capline2").value);
text_write.writeline(document.getElementById("capline3").value);
text_write.writeline(document.getElementById("capline4").value);
text_write.writeline(document.getElementById("capline5").value);
text_write.writeline(document.getElementById("capline6").value);
text_write.writeline(document.getElementById("capline7").value);
text_write.writeline(document.getElementById("capline8").value);
text_write.writeline(document.getElementById("capline9").value);
text_write.writeline(document.getElementById("capline10").value);
text_write.writeline(document.getElementById("capline11").value);
text_write.writeline(document.getElementById("capline12").value);
text_write.writeline(document.getElementById("capline13").value);
text_write.writeline(document.getElementById("capline14").value);
text_write.writeline(document.getElementById("capline15").value);
text_write.Close();
stat_write = fsow.OpenTextFile("file location", 2, true, -2);
stat_write.writeline("CONFIGURED");
stat_write.close();
alert("Saved Caption");
}
function read_caption_view(){
var fsor;
fsor = new ActiveXObject("Scripting.FileSystemObject");
fsor = new ActiveXObject("Scripting.FileSystemObject");
text_read = fsor.OpenTextFile("file location", 1, false, -2);
caption_notice_line1 = text_read.readline();
caption_notice_line2 = text_read.readline();
caption_notice_line3 = text_read.readline();
caption_notice_line4 = text_read.readline();
caption_notice_line5 = text_read.readline();
caption_notice_line6 = text_read.readline();
caption_notice_line7 = text_read.readline();
caption_notice_line8 = text_read.readline();
caption_notice_line9 = text_read.readline();
caption_notice_line10 = text_read.readline();
caption_notice_line11 = text_read.readline();
caption_notice_line12 = text_read.readline();
caption_notice_line13 = text_read.readline();
caption_notice_line14 = text_read.readline();
caption_notice_line15 = text_read.readline();
text_read.Close();
document.getElementById('capline1').innerHTML = caption_notice_line1;
document.getElementById('capline2').innerHTML = caption_notice_line2;
document.getElementById('capline3').innerHTML = caption_notice_line3;
document.getElementById('capline4').innerHTML = caption_notice_line4;
document.getElementById('capline5').innerHTML = caption_notice_line5;
document.getElementById('capline6').innerHTML = caption_notice_line6;
document.getElementById('capline7').innerHTML = caption_notice_line7;
document.getElementById('capline8').innerHTML = caption_notice_line8;
document.getElementById('capline9').innerHTML = caption_notice_line9;
document.getElementById('capline10').innerHTML = caption_notice_line10;
document.getElementById('capline11').innerHTML = caption_notice_line11;
document.getElementById('capline12').innerHTML = caption_notice_line12;
document.getElementById('capline13').innerHTML = caption_notice_line13;
document.getElementById('capline14').innerHTML = caption_notice_line14;
document.getElementById('capline15').innerHTML = caption_notice_line15;
}
function display_caption_view(){
document.getElementById('capline1').innerHTML = caption_notice_line1;
document.getElementById('capline2').innerHTML = caption_notice_line2;
document.getElementById('capline3').innerHTML = caption_notice_line3;
document.getElementById('capline4').innerHTML = caption_notice_line4;
document.getElementById('capline5').innerHTML = caption_notice_line5;
document.getElementById('capline6').innerHTML = caption_notice_line6;
document.getElementById('capline7').innerHTML = caption_notice_line7;
document.getElementById('capline8').innerHTML = caption_notice_line8;
document.getElementById('capline9').innerHTML = caption_notice_line9;
document.getElementById('capline10').innerHTML = caption_notice_line10;
document.getElementById('capline11').innerHTML = caption_notice_line11;
document.getElementById('capline12').innerHTML = caption_notice_line12;
document.getElementById('capline13').innerHTML = caption_notice_line13;
document.getElementById('capline14').innerHTML = caption_notice_line14;
document.getElementById('capline15').innerHTML = caption_notice_line15;
}

Related

Copy/paste a Google Sheets with a loop

I have read all the previous posts and I have a similiar subjetc that I can't solve. I have to copy/paste a Google Sheets (two sheets, 'GENERAL', 'VALEUR') document.
I have won to write a code to copy/paste : 1 document (source) ==to==> 1 document (destination)
function expCalc(){
copypaste_GENERAL();
copypaste_VALEUR();
}
function copypaste_GENERAL() {
var source_G = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1xTBgfI-yy30GHm-LpsUWWoLRACNk5rdc81DPKGyS9fw/edit#gid=0');
var sourceSheet_G = source_G.getSheetByName('GENERAL');
var sourceRange_G = sourceSheet_G.getDataRange();
var sourceValues_G = sourceRange_G.getValues();
var tempSheet_G = source_G.getSheetByName('TEMP_GENERAL');
var tempRange_G = tempSheet_G.getRange('A1:DU11');
var destination_G = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1kFKaNOc8JbRM63tb24QB3_fJms5vnQbZj2lOYsoh0CA/edit#gid=1580219321');
var destSheet_G = destination_G.getSheetByName('GENERAL');
sourceRange_G.copyTo(tempRange_G); // paste all formats?, broken references
tempRange_G.offset(0, 0, sourceValues_G.length, sourceValues_G[0].length)
.setValues(sourceValues_G); // paste all values (over broken refs)
copydSheet = tempSheet_G.copyTo(destination_G); // now copy temp sheet to another ss
copydSheet.getDataRange().copyTo(destSheet_G.getDataRange());
destination_G.deleteSheet(copydSheet); //delete copydSheet
}
function copypaste_VALEUR() {
var source_V = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1xTBgfI-yy30GHm-LpsUWWoLRACNk5rdc81DPKGyS9fw/edit#gid=0');
var sourceSheet_V = source_V.getSheetByName('VALEUR');
var sourceRange_V = sourceSheet_V.getDataRange();
var sourceValues_V = sourceRange_V.getValues();
var tempSheet_V = source_V.getSheetByName('TEMP_VALEUR');
var tempRange_V = tempSheet_V.getRange('A1:I255');
var destination_V = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1kFKaNOc8JbRM63tb24QB3_fJms5vnQbZj2lOYsoh0CA/edit#gid=1580219321');
var destSheet_V = destination_V.getSheetByName('VALEUR');
sourceRange_V.copyTo(tempRange_V); // paste all formats?, broken references
tempRange_V.offset(0, 0, sourceValues_V.length, sourceValues_V[0].length)
.setValues(sourceValues_V); // paste all values (over broken refs)
copydSheet = tempSheet_V.copyTo(destination_V); // now copy temp sheet to another ss
copydSheet.getDataRange().copyTo(destSheet_V.getDataRange());
destination_V.deleteSheet(copydSheet); //delete copydSheet
}
but I can't write a code to copy/paste : 1 document (source) ==to==> MANY (more than 1) documents (destination) according to a list of URLs (here, example only on 2 URLs)
Here is my test code (using only t'GENERAL' sheet for this present test)
function copypaste_GENERAL() {
var source = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1xTBgfI-yy30GHm-LpsUWWoLRACNk5rdc81DPKGyS9fw/edit#gid=0');
var sourceSheet = source.getSheetByName('GENERAL');
var sourceRange = sourceSheet.getDataRange();
var sourceValues = sourceRange.getValues();
var tempSheet = source.getSheetByName('TEMP_GENERAL');
var tempRange = tempSheet.getRange('A1:DU11');
var destSpreadUrl = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1kFKaNOc8JbRM63tb24QB3_fJms5vnQbZj2lOYsoh0CA/edit#gid=1580219321');
var destSheetUrl = destSpreadUrl.getSheetByName('URLTST');
var destSheet_G = destSpreadUrl.getSheetByName('GENERAL');
var urlessai = destSheetUrl.getRange("D2:D3").getValues();
for (var row = 1; row = 3; row++) {
if (urlessai[row] != '') {
sourceRange.copyTo(tempRange); // paste all formats?, broken references
tempRange.offset(0, 0, sourceValues.length, sourceValues[0].length)
.setValues(sourceValues); // paste all values (over broken refs)
copydSheet = tempSheet.copyTo(destSpreadUrl); // now copy temp sheet to another ss
copydSheet.getDataRange().copyTo(destSheet_G.getDataRange());
destSpreadUrl.deleteSheet(copydSheet); //delete copydSheet
};
};
};
Please, can you help me to find a solution to write this copy/paste loop on a list of URLs (for x users) ?
Thanks in advance !
Philippe
This is probably not exactly what you want but I think it's close and I'm willing to tweak it.
function copySrcDes() {
const ss = SpreadsheetApp.getActive();
const locs = [{srcid:"1xTBgfI-yy30GHm-LpsUWWoLRACNk5rdc81DPKGyS9fw",desid:"1kFKaNOc8JbRM63tb24QB3_fJms5vnQbZj2lOYsoh0CA",shts:["GENERAL"]},{srcid:"1xTBgfI-yy30GHm-LpsUWWoLRACNk5rdc81DPKGyS9fw",desid:"1kFKaNOc8JbRM63tb24QB3_fJms5vnQbZj2lOYsoh0CA",shts:["VALEUR"]}];
locs.forEach(obj => {
let sss = SpreadsheetApp.openById(obj.srcid);
let dss = SpreadsheetApp.openById(obj.desid);
obj.shts.forEach(n => {
let ssh = sss.getSheetByName(n);
let dsh = dss.getSheetByName(n);
let vs = ssh.getDataRange().getValues();
dsh.getRange(1,1,vs.length,vs[0].length).setValues(vs);
});
});
}

Add line numbering to a .docx with ActiveXObject('Word.Application')

Hi i want to add line numbering to the opened .docx file before converting it to .pdf and i really didn't find how to do that on code, it is converting my .docx file to pdf but i wanna add line numbering too, am using ActiveXObject with javascript to do that on server
var obj = new ActiveXObject("Scripting.FileSystemObject");
var docPath = WScript.Arguments(0);
docPath = obj.GetAbsolutePathName(docPath);
var pdfPath = docPath.replace(/\.doc[^.]*$/, ".pdf");
var objWord = null;
try
{
objWord = new ActiveXObject("Word.Application");
objWord.Visible = false;
var objDoc = objWord.Documents.Open(docPath);
var format = 17;
objDoc.SaveAs(pdfPath, format);
objDoc.Close();
WScript.Echo("Saving '" + docPath + "' as '" + pdfPath + "'...");
}
catch(e){
WScript.Echo(e);
}
finally
{
if (objWord != null)
{
objWord.Quit();
}
}
If you want to automate Word but you don't know how to do it in code, start recording a macro, do what you want, then stop recording and review the code. You should be able to figure out what you need from the generated code.
Here's an example of a recorded macro:
Sub Macro1
With Selection.PageSetup
With .LineNumbering
.Active = True
.StartingNumber = 1
.CountBy = 1
.RestartMode = wdRestartContinuous
.DistanceFromText = wdAutoPosition
End With
.Orientation = wdOrientPortrait
.TopMargin = InchesToPoints(1)
.BottomMargin = InchesToPoints(1)
.LeftMargin = InchesToPoints(1)
.RightMargin = InchesToPoints(1)
.Gutter = InchesToPoints(0)
.HeaderDistance = InchesToPoints(0.5)
.FooterDistance = InchesToPoints(0.5)
.PageWidth = InchesToPoints(8.5)
.PageHeight = InchesToPoints(11)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.BookFoldPrinting = False
.BookFoldRevPrinting = False
.BookFoldPrintingSheets = 1
.GutterPos = wdGutterPosLeft
End With
End Sub
Based on that example, I would assume your code would need to include this for line numbers:
objDoc.PageSetup.LineNumbering.Active = true;

$ dot each not working for recursion (JS)

I have a loop in which I am calling rec_append() recursively, apparently the first pass alone works, then the loop stops.
I have an array of 4 elements going into that $.each loop but I see only the first element going into the function recursively. Help!
I switched it for a element.forEach but that gives me only the second element and I am stuck, is there a better solution to process a tree of elements? My array is a part of a tree.
var data = JSON.parse(JSON.stringify(result))
var graph = $(".entry-point");
function rec_append(requestData, parentDiv) {
var temp_parent_details;
$.each(requestData, function (index, jsonElement) {
if (typeof jsonElement === 'string') {
//Element construction
//Name and other details in the form of a : delimited string
var splitString = jsonElement.split(':');
var details = document.createElement("details");
var summary = document.createElement("summary");
summary.innerText = splitString[0];
details.append(summary);
temp_parent_details = details;
parentDiv.append(details);
var kbd = document.createElement("kbd");
kbd.innerText = splitString[1];
summary.append(' ');
summary.append(kbd);
var div = document.createElement("div");
div.className = "col";
details.append(div);
var dl = document.createElement("dl");
div.append(dl);
var dt = document.createElement("dt");
dt.className = "col-sm-1";
dt.innerText = "Path";
div.append(dt);
var dd = document.createElement("dd");
dd.className = "col-sm-11";
dd.innerText = splitString[2];
div.append(dd);
var dt2 = document.createElement("dt");
dt2.className = "col-sm-1";
dt2.innerText = "Type";
div.append(dt2);
var dd2 = document.createElement("dd");
dd2.className = "col-sm-11";
dd2.innerText = splitString[1];
div.append(dd2);
} else {
$.each(jsonElement, function (jsonElementArrIndx, jsonChildElement) {
rec_append(jsonChildElement, temp_parent_details); //Only 1 pass works, rest skip
});
}
});
}
rec_append(data, graph);
Sample data:enter image description here

Radio buttons with dat.gui

I am working with the check boxes in three js.I have created a user control panel in which I have added check boxes.What I want to do is when I click on any check box when the other check box is already checked the previous one should be unchecked.So can anyone please tell me how to that.
This is the code that I have written for check boxes.
function addDatGui(){
var gui = new dat.GUI();
parameters = {
a:false,
b:false,
c:false
}
var first = gui.addFolder("Plastic");
var pos1 = first.add(parameters,'a').name('Possitive Charge');
var neg1 = first.add(parameters,'b').name('Negative Charge');
var neu1 = first.add(parameters,'c').name('Neutral');
var second = gui.addFolder("Glass");
var pos2 = second.add(parameters,'a').name('Possitive Charge');
var neg2 = second.add(parameters,'b').name('Negative Charge');
var neu2 = second.add(parameters,'c').name('Neutral');
pos1.onChange(PCharge);
neg1.onChange(Ncharge);
neu1.onChange(NeCharge);
var show = gui.add(parameters,'a').name('Show Charge');
}
You can set .listen() to each controller and .onChange() with a function which will set all parameters to false and then the parameter you need to true:
var gui = new dat.GUI();
parameters = {
a: false,
b: false,
c: false
}
var first = gui.addFolder("Plastic");
var pos1 = first.add(parameters, 'a').name('Possitive Charge').listen().onChange(function(){setChecked("a")});
var neg1 = first.add(parameters, 'b').name('Negative Charge').listen().onChange(function(){setChecked("b")});
var neu1 = first.add(parameters, 'c').name('Neutral').listen().onChange(function(){setChecked("c")});
function setChecked( prop ){
for (let param in parameters){
parameters[param] = false;
}
parameters[prop] = true;
}
jsfiddle example
PS I just didn't get the moment, when you want to use the same object of parameters in two different folders, but it's up to you anyway.

Need help passing a single array value to an attached function on a dynamically created element

I'm having trouble putting an array's value into the function call of an added event on a dynamically created element.
So here's the hard-coded version that works just fine:
var parent_item = document.getElementById("developers_container");
var part = document.createElement('div');
part.id = "developer_A";
part.name = "developer_A";
part.className = "developer_block_un";
part.onmouseover = function() { hilight_dev('A',true)};
part.onmouseout = function() { hilight_dev('A',false)};
parent_item.appendChild(part);
var parent_item = document.getElementById("developer_A");
var part = document.createElement('span');
part.id = "developer_title_A";
part.name = "developer_title_A";
part.className = "developer_un";
part.innerHTML = "John Doe";
part.onclick = function() { select_dev('A')};
parent_item.appendChild(part);
Basically what this does is create a listing of users for selecting. Each user's listing has mouseover, mouseoff and onclick events. The meat of the above code is currently being duplicated for every user.
I want to replace the duplication with an array-based function:
var dev_id = new Array();
var dev_fn = new Array();
var dev_ln = new Array();
document.getElementById("developers_container").innerHTML = "";
dev_id[0] = "A";
dev_fn[0] = "John";
dev_ln[0] = "Doe";
dev_id[1] = "B";
dev_fn[1] = "John";
dev_ln[1] = "Smith";
dev_id[2] = "C";
dev_fn[2] = "John";
dev_ln[2] = "Jones";
dev_id[3] = "D";
dev_fn[3] = "John";
dev_ln[3] = "Yougetthepoint";
document.getElementById("developers_container").innerHTML = "";
for(var i = 0; i < dev_id.length; i++)
{
var parent_item = document.getElementById("developers_container");
var part = document.createElement('div');
part.id = "developer_"+dev_id[i];
part.name = "developer_"+dev_id[i];
part.className = "developer_block_un";
part.onmouseover = function() { hilight_dev(dev_id[i],true)};
part.onmouseout = function() { hilight_dev(dev_id[i],false)};
parent_item.appendChild(part);
var parent_item = document.getElementById("developer_"+dev_id[i]);
var part = document.createElement('span');
part.id = "developer_title_"+dev_id[i];
part.name = "developer_title_"+dev_id[i];
part.className = "developer_un";
part.innerHTML = dev_fn[i]+"<BR>"+dev_ln[i];
part.onclick = function() { select_dev(dev_id[i])};
parent_item.appendChild(part);
}
Every bit of that is working just fine, save for the added events. Where "dev_id[i]" is being used within the function code (as in "part.onmouseover = function() { hilight_dev(dev_id[i],true)};"), it doesn't seem to be doing anything. The event fires off and the function is called, but the variable that's being passed is coming through as "undefined" instead of that user's id like it should be.
I'm hoping someone here knows how to get this to work. Googling was not very helpful, and this is one of those silly little issues that's cost me half a day trying to figure out. Any and all help is greatly appreciated.
Thanks
part.onmouseover = function(i) {
return function() { hilight_dev(dev_id[i],true) }
}(i)
The variable i that you have in the loop is undefined at the time the function is invoked, you need to form a closure over it in order to keep it's value. Please refer to "JavaScript, the Good Parts" by Douglas Crockford for a detailed explanation on this
Err.. after Neil's comment.. yes.. i is not undefined.. it will always be the length of dev_id.. in any case, it's not what the op wants :)

Categories