How to run a single macro for all xls/xlsx files for libreoffice - libreoffice

Is it possible to run a single macro for all xls/xlsx files and if so how. The macro shown below scales the excel file to fit to single page which is necessary as the number of columns is 19 and is needed to convert it to pdf using lo cli.
Libre office version: 6.0.6
Macro has been recorded with libreoffice and can be seen below:
REM ***** BASIC *****
sub Main
rem ----------------------------------------------------------------------
rem define variables
dim document as object
dim dispatcher as object
rem ----------------------------------------------------------------------
vrem get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:PageFormatDialog", "", 0, Array())
end sub
Please let me know if any info is needed regarding the tests.

Got the answer from one of the developers at Libreoffice and it works like a charm, so sharing it here. The link to the answer can be found here
Mike's Solution
First: your recorded macro wouldn't work: it doesn't apply changes, it just opens a dialog. Please always test recorded macros :-)
You may use this macro instead:
Sub FitToPage
Dim document As Object, pageStyles As Object
document = ThisComponent
pageStyles = document.StyleFamilies.getByName("PageStyles")
For i = 0 To document.Sheets.Count - 1
Dim sheet As Object, style As Object
sheet = document.Sheets(i)
style = pageStyles.getByName(sheet.PageStyle)
style.ScaleToPagesX = 1
Next
On Error Resume Next
document.storeSelf(Array())
document.close(true)
End Sub
It operates on the current document, and after setting the scale, it saves (overwrites!) and closes the document.
To use this macro from a command line, you need to save it to some of libraries, e.g. Standard. In my example below, I use Module1 to store it.
You may use this macro on a single document like this:
'path/to/LibreOffice/program/soffice' path/to/excelfile.ext macro:///Standard.Module1.FitToPage
To use it on multiple documents, you need to make this in a loop (mentioning multiple filenames as arguments to a single soffice invocation, like with shell globbing on Linux using *, will not work - actually, it will only run the macro for the last document, keeping the others open and unmodified). A loop for Windows could be like this:
for %f in (*.xls) do start /wait "" "C:\Program Files\LibreOffice\program\soffice.exe" "%f" macro:///Standard.Module1.FitToPage

Related

How to use custom PowerShell functions?

Question about running a custom function in Powershell.
I'm on Windows 10 and I'd like to somehow print my monorepository's directory tree structure excluding node_modules. This is not supported out of the box but requires a custom function to be defined. I found one solution on StackOverflow (https://stackoverflow.com/a/43810460/9654273), which would enable using a command like:
tree -Exclude node_modules -Ascii > tree.txt
The problem is I don't know what to do with the provided source code :D The answer says "add to your $PROFILE, for instance", so I ran notepad $PROFILE in PowerShell, pasted the code snippet there, saved it and tried running the command. It didn't work because I did something wrong. According to the StackOverflow post's comments from anand_v.singh and mklement0 I was still running some other tree command, not the one I just attempted to define.
So how do I use a custom function in PowerShell? Starting point is that source code is on StackOverflow and I don't know where to paste it. Or do you know some other, easier way to print a directory tree on Windows 10 excluding node_modules?
I had the same problem with that function. The issue is the special characters in the hashtable at line 106:
$chars = #{
interior = ('├', '+')[$ndx]
last = ('└', '\')[$ndx] #'
hline = ('─', '-')[$ndx]
vline = ('│', '|')[$ndx]
space = ' '
}
I changed the special characters to ascii as follows:
$chars = #{
interior = ('+', '+')[$ndx]
last = ('\', '\')[$ndx] #'
hline = ('-', '-')[$ndx]
vline = ('|', '|')[$ndx]
space = ' '
}
The only downside is that you do not now have the option of using special graphics characters (the Ascii switch is still there, but does nothing). Maybe someone could tell us how to embed them properly.

VB macro to use a GREP expression to search in a Word Doc

As Word does not have a mechanism for searching based on a regular expression, I was trying to write a simple macro that would, in this case, search in my currently active document for a period (.) WITHOUT a space following it. Here is my first pass on this:
Sub TestREG()
'
' TestREG Macro
'
'
Set objRegExp1 = CreateObject("vbscript.regexp")
objRegExp1.Global = True
objRegExp1.IgnoreCase = True
objRegExp1.Pattern = "\.[A-Z]"
MyDOC = ActiveDocument
objRegExp1.Execute (MyDOC)
End Sub
I know that I'm missing a lot here, but was trying to remember how to do this in an open Word doc. Every test I try, as I step through this is returning False.
Could anyone suggest how I might do this?
Looks like Word can do regular expressions since 2007:
Find and replace text by using regular expressions (Advanced)

Call custom vim completetion menu with Information from Perl-Script

I wrote a script analyzing perl-files (totally without PPI, because it will be used on Servers where the admins don't want PPI to be installed and so on and so forth, but let's not talk about that).
Now, let's say I have this code:
my $object = MySQL->new();
my $ob2 = $object;
$ob2->
(Where MySQL is one of our modules).
My script correctly identifies that $ob2 is a MySQL-Object and sees where it came from, and then returns a list of found subs in that module.
My idea was, that, since I use vim for editing, this could be a really cool way for "CTRL-n"-Completetion.
So, when...
$ob2->[CTRL-n]
It shows the CTRL-n-Box which opens my Perl-Script and gives it a few parameters (I would need: The line that I am actually on, the cursor position and the whole file as it is in vim).
I already found things like vim-perl, which allows me to write something like
if has('perl')
function DefPerl()
perl << EOF
use MyModule;
return call_to_my_function(); # returns all the methods from the object for example
EOF
endfunction
call DefPerl()
endif
But somehow this does not get executed (I tried writing something to a file with a system call just for the sake of testing)...
So, in short:
Does anyone here know how to achieve that? Calling a perl-function from vim by pressing CTRL-n with the full file-code and the line vim is actually in and the position, and then opening a completetion-menu with the results it got from the perl-script?
I hope someone knows what I mean here. Any help would be appreciated.
The details and tips for invoking embedded Perl code from Vim can be found in this Vim Tips Wiki article. Your attempts are already pretty close, but to return stuff from Perl, you need to use Vim's Perl API:
VIM::DoCommand "let retVal=". aMeaningfullThingToReturn
For the completion menu, your Perl code needs to return a List of Vim objects that adhere to the format as described by :help complete-items. And :help complete-functions shows how to trigger the completion. Basically, you define an insert-mode mapping that sets 'completefunc' and then trigger your function via <C-x><C-u>. Here's a skeleton to get your started:
function! ExampleComplete( findstart, base )
if a:findstart
" Locate the start of the keyword.
let l:startCol = searchpos('\k*\%#', 'bn', line('.'))[1]
if l:startCol == 0
let l:startCol = col('.')
endif
return l:startCol - 1 " Return byte index, not column.
else
" Find matches starting with a:base.
let l:matches = [{'word': 'example1'}, {'word': 'example2'}]
" TODO: Invoke your Perl function here, input: a:base, output: l:matches
return l:matches
endif
endfunction
function! ExampleCompleteExpr()
set completefunc=ExampleComplete
return "\<C-x>\<C-u>"
endfunction
inoremap <script> <expr> <Plug>(ExampleComplete) ExampleCompleteExpr()
if ! hasmapto('<Plug>(ExampleComplete)', 'i')
imap <C-x><C-z> <Plug>(ExampleComplete)
endif

How can I interact with ClearCase from Perl?

My project needs couple of things to be extracted from ClearCase data using the Perl script in a excel sheet,those are -
By giving two particular time line or two baseline.
all the activity associated within that baseline (column header "activity")
Owner's id (column header-Owner)
all the element associated within a particular activity. (column header-"element details")
For each element the versions associated (column header-"Versions")
for each element the total number of lines of code,total number of lines of code added,total number of lines of code deleted,total number of lines of code changed..(column header"No. of lines of code","lines of code added","lines of code deleted" & " lines of code changed")
Please kindly help me on this...
Basically, ClearCase Perl scripting is based on parsed outputs of system and cleartool commands.
The scripts are based on a cleartool run cmd like package CCCmd, and used like:
use strict;
use Config;
require "path/to/CCCmd.pm";
sub Main
{
my $hostname = CCCmd::RunCmd('hostname');
chomp $hostname;
my $lsview = CCCmd::ClearToolNoError("lsview -l -pro -host $hostname");
return 1;
}
Main() || exit(1);
exit(0);
for instance.
So once you have the basic Perl structure, all you need is the right cleartool commands to analyze, based on fmt_ccase directives.
1/ all the activity associated within that baseline (column header "activity")
ct descr -fmt "%[activities]CXp" baseline:aBaseline.xyz#\ideapvob
That will give you the list of activities (separated by ',').
For each activity:
2/ Owner's id (column header-Owner)
ct descr -fmt "%u" activity:anActivityName#\ideapvob
3/ all the element associated within a particular activity. (column header-"element details")
Not sure: activities can list their versions (see /4), not easily their elements
4/ For each element the versions associated (column header-"Versions")
For a given activity:
ct descr -fmt "%[versions]CQp\n" activity:anActivityName#\ideapvob
5/ for each element the total number of lines of code,total number of lines of code added,total number of lines of code deleted,total number of lines of code changed..(column header"No. of lines of code","lines of code added","lines of code deleted" & " lines of code changed")
That can be fairly long, but for each version, you can compute the extended path of the previous version and make a diff.
I would advise using for all that a dynamic view, since you can access any version of a file from there (as opposed to a snapshot view).
Also if you need to use perl with Clearcase have a look at the CPAN module ClearCase::CtCmd. I would recommend to use this perl module for invoking clearcase commands.
For the CCCmd package, I had to remove the double-quotes in the RunCmd and RunCmdNoError subs to get it to work.

Tool to merge MS Word files

I have huge number of Word files I need to merge (join) into one file, and will be time consuming to use the Word merger (one by one). Have you experienced any tool that can handle this job?
Sub MergeAllDocuments(AllDocumentsPath as String, MasterDocumentPath as String)
Dim MasterDocument As Document
Set MasterDocument = Documents.Open(FileName:=MasterDocumentPath)
TheDocumentPath = Dir(AllDocumentsPath , vbNormal)
While TheDocumentPath <> ""
' Append the next doc to the end of the master doc. (The
' special "\EndOfDoc" bookmark is always available!)
MasterDocument.Bookmarks("\EndOfDoc").Range.InsertFile TheDocumentPath
TheDocumentPath = Dir
Wend
MasterDocument.Save
End Sub
MergeAllDocuments "C:\MySeparateDocuments\*.doc", "C:\MasterDocument.doc"
I have one question - why do you want do do such a thing (with a "huge number" of documents, at least)?
I came across a post by Graham Skan a while back. It might get you started:
Sub InsertFiles()
Dim strFileName As String
Dim rng As Range
Dim Doc As Document
Const strPath = "C:\Documents and Settings\Graham Skan\My Documents\Allwork\" 'adjust as necessary '"
Set Doc = Documents.Add
strFileName = Dir$(strPath & "\*.doc")
Do
Set rng = Doc.Bookmarks("\EndOfDoc").Range
If rng.End > 0 Then 'section break not necessary before first document.'
rng.InsertBreak wdSectionBreakNextPage
rng.Collapse wdCollapseEnd
End If
rng.InsertFile strPath & "\" & strFileName
strFileName = Dir$()
Loop Until strFileName = ""
End Sub
Have you tried using the Word COM api? You can automate lots of things - maybe you can automate a merge.
Do you really need to do an actual merge, or do you want to join the files together. The two things are quite different.
Merging is used when you have two versions of an original file with (potentially conflicting) changes. I can't really see how you would have a "huge number" of files that you needed to merge all together. This would be an absolute nightmare of conflicts. Do you mean to merge sets of them into individual files?
Joining would be when you want to concatenate them one after the other. This would be a lot easier to do. This is quite possible using the COM api.