Monday, May 24, 2021

Opening a pdf as a Google Document using google apps script

This link has the steps to make Google apps script do OCR on the PDF - 

const blob = DriveApp.getFileById(fileID).getBlob();
  const resource = {
    title: blob.getName(),
    mimeType: blob.getContentType()
  };
  const options = {
    ocr: true,
    ocrLanguage: "en"
  };
  // Convert the pdf to a Google Doc with ocr.
  const file = Drive.Files.insert(resource, blob, options);

But this generally gave pretty terrible results for me, and the formatting was completely lost.  

For my use case, found that the PDF was being created from HTML, so direct conversion from HTML to GDoc gave good results - 

assethtml += contentdata;
var ablob = Utilities.newBlob(assethtml, MimeType.HTML, "asset.html");
var AssetGDocId = Drive.Files.insert(
      { title: 'The name of the document', 
      mimeType: MimeType.GOOGLE_DOCS, parents: [{"id": destFolderID}] },
      ablob ).id;


Wednesday, May 19, 2021

appending Google docs and adding header and footer

In the current implementation, the adding of header and footer is done by making a copy of an existing template doc which has the required header and footer, and then appending the required contents inside it. 

Unfortunately, with the (slow) template method, Google Apps script seems to have difficulty with importing more than 2-3 chapters at a time, where each chapter consists of around 10-15 Google Docs which need to be concatenated. Each chapter takes 5 to 10 minutes, and we reach the limits of processing time + parsecsv's max char limit. 



Saturday, May 08, 2021

glitch.com for building web apps quickly

An interesting service to build Node.js web apps by "remixing" available code - 

https://blog.glitch.com/post/google-docs-markdown-glitch

https://help.glitch.com/kb/article/17-what-are-the-technical-restrictions-for-glitch-projects/ - 1000 free project hours per month, 200 MB disk space, 4000 requests per hour.

https://flaviocopes.com/glitch/ has some interesting use cases - teaching, timed webhook and so on.

Tuesday, May 04, 2021

calling a web app using curl in php

Just the syntax to use in php for using curl and making a GET or POST request to a google apps script - a slight modification from the answer given here, as curl_close was not needed for us - 

$url = 'https://script.google.com/macros/s/ID_GOES_HERE/exec?optionname=OptionValue';
$schName = 'Some test';
$url = $url . urlencode($schName);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl,CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($curl);
echo $response;

Monday, May 03, 2021

enabling desktop app notifications on linux

I had mistakenly clicked on "Don't show this message again" or something like that, and wanted to re-enable the notification shown when a VPN connection is made (or fails). The way to do it was mentioned at https://forums.linuxmint.com/viewtopic.php?t=89627

sudo gsettings set org.gnome.nm-applet disable-connected-notifications false sudo gsettings set org.gnome.nm-applet disable-disconnected-notifications false sudo gsettings set org.gnome.nm-applet disable-vpn-notifications false

file written to google drive by service account

When using a service account to create a new file and add it to Google drive, the file owner is the service account.

Can transfer ownership using Google Apps Script also.

https://stackoverflow.com/questions/46680352/how-can-i-change-the-owner-of-a-google-sheets-spreadsheet

https://stackoverflow.com/questions/65256980/google-drive-api-v3-php-client-transfer-file-ownership


restarting remote desktop via ssh or azure portal

Bs series burstable Azure VMs seem to have an endemic problem of the remote desktop service dying or refusing connections after the VM sees some heavy loads. Initially I had no idea why RDesktop was not connecting ... anyway, a couple of workarounds - 

  1. install ssh server and leave the ssh service running
    ssh into the machine when RDP has issues,
    ssh  adminuser@server.tld -p port
    powershell
    get-service termservice
    get-service termservice -dependentservices

    stop-service UmRdpService
    restart-service termservice
    start-service UmRdpService


  2. install Windows Admin Center on the VM and connect via the Azure portal, restarting the same services as above. We need to use the "Connect with Public IP address" method on the Azure portal. 




Sunday, May 02, 2021

limits for google apps scripts execution

A six-minute script execution time is mentioned at
https://developers.google.com/apps-script/guides/services/quotas (also many more limits)
but if we run the script from the Script Editor, the limit is 30 minutes. Probably since our Google Workspace is a non-profit one, so the "paid" limit applies. And a programmatic workaround (which I've not used till now.)

Saturday, May 01, 2021

handling duplicates in an sql query

There was a database in which we needed to join three tables, asset id -> subtheme id -> subtheme and the subtheme ids had duplicates as well as multiple subthemes mapped to an asset id. Getting a go-ahead from the content lead, the solution chosen was to just take a single subtheme, using select distinct - something like the first method at https://www.sisense.com/blog/4-ways-to-join-only-the-first-row-in-sql/  

left join (select distinct on (asset_id) * 
         from asset_subtheme_xref
     order by asset_id asc
) as asx on asx.asset_id = tma.asset_id
        left join subtheme_master lsm on lsm.subtheme_id = asx.subtheme_id

Edit: Later on, multiple subthemes were desired. Then, instead of using select distinct in this way, we would need some sort of subquery similar to the one used below for another field, using concat to add all the different subthemes together with CHR(10) - linefeed - between them - all enclosed in parentheses - 

(select string_agg(concat(btwo.name,',',stwo.name,',',sutwo.NAME,',',ctwo.name), CHR(10))
as "Asset tagged to Board Standard Chapter"
from asset_bssc_xref tabxr 
left join chapters ctwo on ctwo.chapter_id = tabxr.chapter_id
left join board_master btwo on btwo.board_id = ctwo.board_id 
left join standard_master stwo on stwo.standard_id = ctwo.standard_id
left join subject_master sutwo on sutwo.subject_id = ctwo.subject_id 
where 
tabxr.asset_id = tma.asset_id) as "Asset Tagged to"


installing ssh client and server on Windows Server 2016

https://hostadvice.com/how-to/how-to-install-an-openssh-server-client-on-a-windows-2016-server/

Or maybe needs this,

https://github.com/PowerShell/Win32-OpenSSH/wiki/Install-Win32-OpenSSH

or other solutions listed at

https://docs.microsoft.com/en-us/answers/questions/79182/sshd-and-sshd-agent-service-not-appearing-after-in.html

or maybe the powershell method listed at

https://docs.microsoft.com/en-us/windows-server/administration/openssh/openssh_install_firstuse