Bug 99650 - Control line endings with print statement
Summary: Control line endings with print statement
Status: RESOLVED WORKSFORME
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: BASIC (show other bugs)
Version:
(earliest affected)
unspecified
Hardware: All All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-05-03 14:10 UTC by s.p.helma
Modified: 2016-05-10 09:42 UTC (History)
3 users (show)

See Also:
Crash report or crash signature:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description s.p.helma 2016-05-03 14:10:30 UTC
I have a Basic macro which writes the contents of a spreadsheet into a text file. This macro was developed on a LO installed on Windows. Now I switched to Linux and have a problem with the line endings in the generated file.

The file must have Windows line endings (\r\n or chr(13)+chr(10)), because it is used in other programs which insist on these line endings.

I tried a couple of approaches to save the \r\n line endings on a Linux installation:

--8<---
sub ExportAreaToFile
  ' This prints the range "Sheet1.A1:C3" with two different methods into two different text files.

  dim oSheet as object
  dim datarangeAddress as object
  dim sCell as string
  dim sTab as string
  dim sLF as string
  dim i, j as long
  ' ALTERNATIVE B:
  dim result as string

  sTab = chr(9)
  sLF = chr(13) + chr(10)

  ' Open file descriptors
  iNumA = FreeFile
  iNumB = FreeFile
  Open "output_A.txt" for Output as #iNumA
  Open "output_B.txt" for Output as #iNumB

  ' Get range
  oSheet = ThisComponent.Sheets.getByName("Sheet1")
  datarangeAddress = oSheet.getCellRangeByName("A1:C3").RangeAddress()

  ' Loop over our range
  for j = datarangeAddress.StartRow to datarangeAddress.EndRow
    for i = datarangeAddress.StartColumn to datarangeAddress.EndColumn
      sCell = oSheet.getCellByPosition(i, j).string
      ' ALTERNATIVE A:
      print #iNumA, sCell; sTab;
      ' ALTERNATIVE B:
      result = result + sCell + sTab
    next i
    ' ALTERNATIVE A:
    print #iNumA, sLF
    ' ALTERNATIVE B:
    result = result + sLF
  next j

  ' ALTERNATIVE B:
  print #iNumB, result

  close #iNumA
  close #iNumB
--8<---

Both alternatives do not put \r\n in the output files, despite explicitly adding these characters with print #iNumA, sLF or result = result + sLF.

See also http://en.libreofficeforum.org/node/13243, where I asked how I can control the line endings.

Stephan
Comment 1 Buovjaga 2016-05-07 11:38:52 UTC
I think you should ask at https://ask.libreoffice.org/ as well.
Comment 2 s.p.helma 2016-05-09 10:05:51 UTC
Hi Buovjaga,

Thanks for your comment. Here's the link to my question: https://ask.libreoffice.org/en/question/69560/control-line-endings-with-print-statement/
Comment 3 raal 2016-05-10 07:59:16 UTC
(In reply to s.p.helma from comment #2)
> Hi Buovjaga,
> 
> Thanks for your comment. Here's the link to my question:
> https://ask.libreoffice.org/en/question/69560/control-line-endings-with-
> print-statement/

Hello,
question is answered. Let us know if it works.
Comment 4 s.p.helma 2016-05-10 09:36:07 UTC
Hello,

Yes, that answer worked. Great.

Still I find it a bit puzzling that the hand-crafted line endings are overwritten when the file is opened as "Output". That might be the default in Basic (I don't know), but at least that should be mentioned in the documentation. Can that be done, preferably with a link to the example in the PUT statement and a hint that the correct line endings can be written with a combination of chr(13) and chr(10)?
Comment 5 Buovjaga 2016-05-10 09:42:59 UTC
(In reply to s.p.helma from comment #4)
> Still I find it a bit puzzling that the hand-crafted line endings are
> overwritten when the file is opened as "Output". That might be the default
> in Basic (I don't know), but at least that should be mentioned in the
> documentation. Can that be done, preferably with a link to the example in
> the PUT statement and a hint that the correct line endings can be written
> with a combination of chr(13) and chr(10)?

You should join the documentation team and submit a patch for the help:
https://wiki.documentfoundation.org/Documentation
https://wiki.documentfoundation.org/Website/IRC
http://www.libreoffice.org/get-help/mailing-lists/#documentation