Forum Moderators: open

Message Too Old, No Replies

Coding Alt+Enter in Javascript

Copy/Paste Alt+Enter from Javascript to Excel

         

VtForMe

2:32 pm on Aug 12, 2008 (gmt 0)

10+ Year Member



What I have is a web page written in javascript compiled into an htm file. The stand alone web page allows customers to wade through complicated electronics by answering questions and then the page prompts them with the correct item to order.

Once they have configured the item, there is a link that says "copy to clipboard - excel format". This has been working fine except that now there is a request to put a line feed (alt + enter) after 1 bit of text and before the next to keep is all in the same excel cell but on different lines. The ultimate goal of all this is to paste from the clipboard into a formatted excel spreadsheet to create in order form.

I've tried to insert "\v" (vertical tab) but that does nothing.
I've tried to insert "\n" (line feed) but that moves to the next row to paste the rest of the data.
I've tried to insert "\10" but that inserts a square box instead of the line feed.
I've tried to insert String.fromCharCode(10) but that moves to the next row also.

How do I do this? See sample code below.

data = data + Qty;
data = data + "\t" + designation;
data = data + "\t" + description;
data = data + "\v" + NeedNewLine;
data = data + "\t" + Net;
data = data + "\r\n";

window.clipboardData.setData('Text', data);

What I need is for the text in "Description" to show first in the cell. Then in the same cell on a new line show the text called "NeedNewLine".

Any suggestions are appreciated.

DrDoc

7:43 pm on Aug 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld!

You have to do it by creating TSV (tab separated values) data.

foo[TAB]"bar baz 
foo bar"[TAB]123
"foo ""bar"" baz"[TAB]loi loi[TAB]456

VtForMe

11:52 am on Aug 13, 2008 (gmt 0)

10+ Year Member



DrDoc,
Thank you for responding. I am new to JavaScript and do not understand your answer. Per your answer, I have tried the following with poor results.
data = data + "\[TAB]" + NeedNewLine;
data = data + "[TAB]" + NeedNewLine;
data = data + string.fromCharCode([TAB]) + NeedNewLine;
The last one gives an error. The first 2 insert the word "TAB" into the cell where I want to insert ALT+ENTER for a new line.

Did I misunderstand your response?

Thanks.

DrDoc

6:20 pm on Aug 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Replace "[TAB]" with "\t" or an actual tab.

VtForMe

11:20 pm on Aug 13, 2008 (gmt 0)

10+ Year Member



Thanks, but if you look at the example above, you will see that I've used that code. "\t" as you say is a tab and when pasted into a cell will move the next data into the next cell to the right. What I need is an ALT+ENTER to start a new line within the same cell.
Thanks any way,
VtForme

daveVk

2:34 am on Aug 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think you need quotes around fields containing newlines or tabs.

eg
data = data + "\t" + '"' + description + '"';

you will probably also need to replace any quotes within desciption with ""

If all else fails try exporting example from excel to discover formatting required for new lines within a field.

DrDoc

3:16 am on Aug 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Export the data from Excel as "tab separated values" file. That's what you need yours to look like, if you want them to be able to copy and paste without much trouble.

VtForMe

12:28 pm on Aug 14, 2008 (gmt 0)

10+ Year Member



Thanks to both of you, but none of this is working.
Extra quotes had no affect at all.
I can "save as" from excel to various formats but is does not show me anything useful.

Maybe I should try to automatically convert the  that I get to the correct alt+enter once it is within the excel spreadsheet. I'm not sure how I can do that, but I'll give that a try.

Thanks again.

DrDoc

8:30 pm on Aug 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The box should convert to a newline if you set the cell format to line wrap.

VtForMe

9:05 pm on Aug 14, 2008 (gmt 0)

10+ Year Member



The cell has always been in line wrap mode.
Thanks anyway.

DrDoc

10:42 pm on Aug 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So, what you are saying is that something like this (again, replace "[TAB]" with an actual tab):
foo[TAB]"bar 
baz"[TAB]foobar

...does not result in:

+-----+-----+--------+ 
¦ foo ¦ bar ¦ foobar ¦
¦ ... ¦ baz ¦ ...... ¦
+-----+-----+--------+

Right?

[edited by: DrDoc at 10:43 pm (utc) on Aug. 14, 2008]

VtForMe

11:17 pm on Aug 14, 2008 (gmt 0)

10+ Year Member



No it does not.
It results in:

+------+---------+-------+
¦ foo ¦ bar baz ¦ foobar¦

I can not do exactly what you suggest using Visual Web Developer 2005. It results in an error when I try to run the web page. I can not do:
data = data + " " + newline1;
data = data + "
" + newline2;

This creates an error on the web page.

DrDoc

12:43 am on Aug 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No need to. Use "\n" in place of the actual newline. As long as the output is what I displayed above, it really doesn't matter how it is generated.

VtForMe

12:46 am on Aug 15, 2008 (gmt 0)

10+ Year Member



"\n" successfully starts a new line in excel.
"\t" successfully tabs to a new cell in excel.
What I need is Alt+enter to start a new line within the same cell. NOT another TAB.

DrDoc

12:50 am on Aug 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Put this in your script:

var strOut = "foo\t\"bar\nbaz\"\tfoobar"; 
window.clipboardData.setData('Text', strOut);

Now paste in Excel. Voila!

var strOut = "foo\t\"bar\nbaz\"\t\"I can even \"\"quote\"\" strings in here!\""; 
window.clipboardData.setData('Text', strOut);

That works too!

As long as you have tab characters (\t) separating the fields, and any fields which employ newlines surrounded by double quotes -- it should work.

Again, create what you want in Excel. Save the file in the "Text (Tab delimited)" format. Open the file in your favorite text editor. That is what you need to recreate in your JavaScript. And that is what we have given you instructions for above.

var strOut = "foo\t\"bar\nbaz\"\tfoobar"; 
window.clipboardData.setData('Text', strOut);

That works.

[edited by: DrDoc at 12:51 am (utc) on Aug. 15, 2008]

daveVk

1:12 am on Aug 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I can "save as" from excel to various formats but is does not show me anything useful.

Are you saying that when you "save as tab separated values" the file contains some odd character at the line break position ?

The odd character in you post appears to be "/010" octal 10, decimal 8 (backspace) perhaps that is it ?

VtForMe

12:24 pm on Aug 15, 2008 (gmt 0)

10+ Year Member



OK, I am finally seeing the results that I need. However, this is with dummy text. Now I am struggling with getting the + and " in the right places as I try to substitute the dummy text with variables.

Could I beg one more try? Please replace foo bar baz and foobar with variable names Qty Description Newline and Net.

Sorry to be so dense, but I have been looking at this so long, I'm not seeing the forest for the trees.

Thanks for sticking with me!

VtForMe

12:47 pm on Aug 15, 2008 (gmt 0)

10+ Year Member



I FINALLY GOT IT !

data = Qty + "\t" + "\"" + description + "\n" + newline + "\"\t" + Net;

THIS WORKS PERFECTLY !

Thanks again.