Created PDF does not reflect changes made to the original document - templates

I have a Google Docs template which is automatically copied into a folder and replace specific values with values coming from spreadsheet cells.
The template contains values such as <<41>> which are used to be "find-and-replaced" with values coming from a spreadsheet.
The find-and-replace process is fine, and the results of the document looks like this image below
Now, I want this document to be converted into PDF after it has been updated, so I made a function for the convertion and call it at the bottom after all the codes has been executed.
Here's the function:
//convert to PDF
function convertPDF(FileID,newName) {
Utilities.sleep(120000);
docblob = DocumentApp.openById(FileID).getAs('application/pdf');
/* Add the PDF extension */
docblob.setName(newName + ".pdf");
var file = DriveApp.createFile(docblob);
}
The convertion works fine, but the converted document isn't updated. Rather, it is like it was the one freshly copied from the template, before the values were changed.
If you may notice, I have added a "sleep" timer before in the conversion function so as to delay the conversion and give time for the changes to be saved, I've tried 1 and 2 minutes sleep but still it doesn't work.
What can I do to make sure the PDF is created from the updated template?

The function I provided above works fine, we just need to forced the script to save the changes by calling the saveAndClose() method before calling the function that converts the doc into PDF.

I was searching for an answer for this same problem but for Google Sheets. Gave saveAndClose() a shot, but it didn't work for Sheets. Did some more searching and found that the Sheets solution for a similar problem is:
SpreadsheetApp.flush()
Here is the relevant Question/Answer.
Hope this helps someone get to their answer faster.

Related

How to prevent a 'hidden' link to an image within a #if condition in MediaWiki

We have an image which has a predictably-positioned number in the filename. I handle the display of the image in a template based on whether or not the number is within multiple different ranges. If it is in any of those ranges, the image does not (and should not) exist.
Everything works fine, but I find that Special:WantedFiles still includes a listing for the intentionally 'missing' images. The sole link for each 'missing' file is from the page that is using that template. It seems that, even though the page correctly isn't trying to display the image, the wiki still is also interpreting the [[File:foobar]] bit as if the #if test were resolving the other way, creating an unused link to a non-existant file.
The following is a simplified version of the problem part of the template. ImageRangeTest is the range check template; it works fine, returning either a 0 or a 1 depending on if the {{{1}}} number is in any of the matching ranges.
{{#ifeq: {{ImageRangeTest | {{{1}}} }} | 1 | This image does not exist. | [[File:{{{1}}}.png]] }}
So why, when ImageRangeTest returns 1, does "This image does not exist." properly get displayed, but File:{{{1}}}.png still get a hidden link from that same page, causing File:{{{1}}}.png to show up on Special:WantedFiles?
What you want do is not possible currently. It's a 'hidden' link to you, but a normal link to the database. The table storing file inclusions does not differentiate on whether the image exists or not. Special:WantedFiles generates its content by querying that table among others.
Various requests to change this were declined as per back as 2006, such as this and this. Not the request were mostly about general links generated from templates but the underlying problem/issue is the samae as yours.
The only way to stop it from showing in that special page is to delink the name, that's to remove the square brackets, and I doubt if you'd like that.

Drupal 8 Webform: how to display text input on one page on the next page?

I am trying to develop a multistep webform in Drupal 8 using Webform 8.x-5.1. I have written a WebformHandler that extends Drupal\webform\Plugin\WebformHandlerBase and made it available to the webform.
In the first step of the webform, I collect a text-field. I would like to display the value of that text-field in an HTML element (Advanced HTML/Text or Basic HTML) on the second page after doing some computation.
I have overwritten submitForm() in the WebformHandler and in it assign the value I want to the HTML element as follows:
$form['elements']['page_name']
['advanced_html_element']['#text'] = '...my HTML...';
Using ksm() I can see that this assignment works, but the the HTML element is not rendered with my HTML: the element is either invisible or contains the initial value set up in the form editor.
Clearly I'm missing something. Should I be using something other than submitForm? Can anyone help me?
It's been a long haul, but I've finally worked out how to do what I want to. The following works for me.
Firstly, I discovered the method validateForm in WebformHandlerBase. On each page in a form with multiple pages, you will find that the following methods are called in the order given here:
submitForm (called once)
alterForm(called possibly more than once)
validateForm (called once)
The name validateForm leads me to believe I may be misusing this method, but that is where I set up the elements on the following page that I wish to programmatically initialise. It works, so what the hey!
In validateForm, I initialise the elements that appear on the following page as follows:
$form_state->setValue(<element name>, <data structure>);
The <element name> is the name you give the element in the form editor ("Build" tab). The <data structure> has to be correct, of course: I suggest you find the appropriate structure by first filling in the element on the next page manually and seeing what turns up in $form_state.
There is also a $form_state->getValue(<element name>), which seems to me to mean that $form_state can also be used for storing session data, say in hidden fields. I initially used Drupal::service('tempstore.private')->get('xxx') for storing data that had to be available across page boundaries, but $form_state might be a cleaner solution.
I hope this helps someone: I spent a horribly long time trying to get this to work.

add macro to excel using python xlswriter with data in original excel

I'm using the following code to add a Macro to Excel. I notice that the data / other WorkSheets from the original Excel had dropped completely but the Macro is showing.
This is the code that I am using:
import xlsxwriter
workbooks = xlsxwriter.Workbook('C:\\Users\\user\Desktop\\test.xlsm')
workbooks.add_vba_project('C:\\Users\\user\\Desktop\\vbaProject.bin')
workbooks.close()
I used the link http://xlsxwriter.readthedocs.io/example_macros.html and it seems to be the same from another page https://redoakstrategic.com/pythonexcelmacro/
I wondered if there is another library that I should use for this?
I tried the following link Use Python to Inject Macros into Spreadsheets it seems that here again the data from the original file is overwritten. Not sure if this is a duplicate or not, or if I missed something rather obvious ?
Thanks
Unfortunately, xlsxwriter can't load information from already existing Excel workbooks; it is only used for making new ones. You are overwriting your old workbook with a blank one that has your macros.
If you need to load information, look into openpyxl. It can be used for creating .xlsm files.
One way around it is to create the macro you want in another Excel so we can execute it to affect the other Excel. Then using win32.com it runs the VBA.
enter code here
#import win32com.client
#xl=win32com.client.Dispatch("Excel.Application")
#xl.Workbooks.Open(Filename="C:\\macro.xlsm",ReadOnly=1)
#xl.Application.Run("macro")
#xl = 0

OpenOffice/LibreOffice calc hyperlink

I'm using OpenOffice calc (or rather the Libreoffice verion in Ubuntu 11.04). I used the hyperlink function to add links to a column using the function: =hyperlink("http://www.ncbi.nlm.nih.gov/pubmed?term="&D2,D2)
All appears okay on the spreadsheet, but when I save as html, the links column is saved as plain text and not as hyperlinks to where I want them to go to!
Am I doing something wrong?
Thanks!
You could report a bug, but it looks like the underlying problem is that the spreadsheet formulas are removed when saving to html, something as simple as A1*3 is not converted to javascript, and the hyperlink() function isn't converted either. If you insert a link with Insert -> Hyperlink it should work fine.

How to override the default text in MATLAB

In MATLAB, when you click File -> New -> Function M-File, you get a file with the following contents:
function [ output_args ] = Untitled( input_args )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
end
Is it possible to override this behaviour, and specify your own text?
(The motivation is that I'm trying to persuade my colleagues to document their m-files more thoroughly, and having default text for them to fill in might encourage them.)
I didn't even know File->New->Function did that.
The way I solved the issue was to write a function that you call via
>>newFunction myNewFunctionName
It then
pops up an inputdlg window, which asks the user for the synopsis and the H1 line and allows to already write help to explain input and output arguments. There, the user also selects whether myNewFunctionName is a function or a class in order to choose the right header and 'function call'
checks whether a function of the same name exists already
asks for a folder to save the function, and
opens the function in the editor
The header is set up so that it's easy to fill in info about input and output. It also automatically lists the username of the person who created the file as well as the date and the Matlab version.
EDIT
For new classes, the template function automatically makes sure that they subclass my general superclass that implements methods such as 'help' (which calls doc(class(obj)) )
Now if the template functionwould also write the algorithm part of the function, it would be really convenient. :)
EDIT2
Here's a link to the function on the file exchange.
I would suggest making your own default m-file template, called default.m for example, and placing it in a folder on the MATLAB path where your colleagues can access it. You should then set the file to be read-only. Your colleagues can then execute any one of the following commands in the MATLAB Command Window when they want to create a new function m-file:
open default.m
open('default.m')
edit default.m
edit('default.m')
The functions OPEN and EDIT will open a file in the MATLAB Editor. Since the file default.m is read-only, if anyone tries to save over it they will get a dialog box warning them as such and asking them to save to a new file (or overwrite it). That should keep them from accidentally modifying the template.
I searched through all text files starting from matlabroot folder, but could not find that template. Seems it's hard-coded, which is weird.
I like Jonas approach. As my two cents, you can download a function (not mine) doing similar things with some customization from here.
After more pondering, I've come up with a solution that I'm happy with, combining Jonas' and gnovice's answers. It's a function that creates a new m-file (with template documentation), and opens it in the editor. It is available from the Matlab Central File Exchange.