How to Include CSS in import.io Connect Extract - html

Using import.io connector, I was able to extract a segment of html from the source web site. This result is returned as "html" type. The result is a single table of data with styles defined in the body html but not extracted. This resulted in the html segment extracted displayed with NO style and looking terrible.
Is there a way to INCLUDE extracting CSS styles, i.e. multiple css hrefs included in the source html, like
<link rel="stylesheet" href="http://cdn.ideamelt.com/1.3/css/ideamelt.min.css">
Also at the same time to include dynamic css like the following:
<style type="text/css">
#financials-iframe-wrap {
width: 635px
}
.td_genTable table {
border: none
}
tr.net {
font-weight: bold;
border-top: 1px solid #009EC2
}
.td_genTable td {
border: 0;
padding: 0
}
a.h3-link {
color: #ffffff;
text-decoration: underline;
float: right
}
</style>
... in the connector extract so that the resultant html segment can be properly styled and displayed?
Thanks in advance!

This is a fairly interesting use case.
You can extract the link and style elements as html using a custom xpath such as //link and //style
You can then output them into your page HTML and that will import the css documents from the pages and should include the styling.
(Be aware that the website in question may not want you to be taking their css and using it on a different website, so they may block downloads of css to websites hosted in different domains)

Sorry, I'm not familiar with Import.io.
Is there way to get refs from links and content from styles? Are you using javascript?
If so, then you may use the folowing js functions to include your styles into the target document:
// Include css from 'style' tag
function include_css (src) {
var _head = document.head || document.getElementsByTagName('head')[0] || document.documentElement,
style = document.createElement ('style');
style.setAttribute ('type', 'text/css');
if (style.styleSheet){
style.styleSheet.cssText = src;
} else {
style.appendChild (document.createTextNode (src));
}
_head.appendChild (style);
}
// Include css referred by 'link' tag
function include_link (ref) {
var _head = document.head || document.getElementsByTagName ('head')[0] || document.documentElement,
style = document.createElement ('link');
style.setAttribute ('rel', 'stylesheet');
style.setAttribute ('type', 'text/css');
style.setAttribute ('href', ref);
_head.appendChild (style);
}

Related

How to instantly print marked columns in google sheets with script?

I have file in google sheets(img 1).
https://i.stack.imgur.com/1j8ny.png [1]
i wrote script which is converting marked columns to word (i need to mark only 1st 4 columns becuase its going to be on sticker) (img 2)
https://i.stack.imgur.com/GnIXo.png [2]
this is the code:
SpreadsheetApp.getUi()
.createMenu('Drukowanie odpadów')
.addItem('Drukuj zazaczone odpady - ZAZNACZ 4 pierwsze kolumny!', 'sprawdz')
.addToUi();
/**
* #NotOnlyCurrentDoc
*/
function sprawdz() { //zbiera dane z zaznaczenia (4 kolumny) w arkuszu GoogleSheet, uzupełnia GoogleDoc danymi
// otworzenie dokumentu w google Doc, do którego będą przenoszone zaznaczone kolumny
var doc = DocumentApp.openByUrl("https://docs.google.com/document/d/WORD_ID/edit");
var body = doc.getBody();
//CZYSZCZENIE ORAZ USTAWIENIE WYMIARÓW ETYKIETY
body.clear();
body.setPageHeight(100);
body.setPageWidth(260);
body.setMarginLeft(0.1);
body.setMarginRight(0.1);
body.setMarginTop(0.01);
body.setMarginBottom(0.01);
var sheet = SpreadsheetApp.getActiveSheet();
var selection = sheet.getSelection();
var data = selection.getActiveRange().getValues();
//PIERWSZE UZUPEŁNIENIE DOKUMENTU
body.getParagraphs()[0].appendText(' {nr_odpadu} {dlugosc}').setFontSize(13);
body.appendParagraph(' {kolor}').setAlignment(DocumentApp.HorizontalAlignment.LEFT).editAsText().setFontSize(10);
body.appendParagraph(' {profil}').setAlignment(DocumentApp.HorizontalAlignment.LEFT).editAsText().setFontSize(10);
body.appendParagraph(' ').setAlignment(DocumentApp.HorizontalAlignment.LEFT).editAsText().setFontSize(35);
//PETLA KTORA ZBIERA DANE Z ARKUSZA I UMIESZCZA JE W ODPOWIEDNIM MIEJSCU W DOKUMENCIE
for (var i = 0 ; i < data.length; i++) {
body.replaceText('{nr_odpadu}', data[i][0]);
body.replaceText('{profil}', data[i][1]);
body.replaceText('{kolor}', data[i][2]);
body.replaceText('{dlugosc}', data[i][3]+'mm');
//jeśli dotrzemy do końca listy, to nie ma potrzeby dalszego drukowania
if (i != data.length-1){
body.appendParagraph(' {nr_odpadu} {dlugosc}').setAlignment(DocumentApp.HorizontalAlignment.LEFT).editAsText().setFontSize(13);
body.appendParagraph(' {kolor}').setAlignment(DocumentApp.HorizontalAlignment.LEFT).editAsText().setFontSize(10);
body.appendParagraph(' {profil}').setAlignment(DocumentApp.HorizontalAlignment.LEFT).editAsText().setFontSize(10);
body.appendParagraph(' ').setAlignment(DocumentApp.HorizontalAlignment.LEFT).editAsText().setFontSize(35);
}
}
}
is there any option to insta print with printer the word document from google sheets script editor?
F.e i mark 4 columns i run the script and it print me the sticker. Im using printer "Zebra GK420t"
Im converting it to word document because when i was converting it to pdf i couldnt change page size to height 100 and weight 260 and i dontk now how to place the things like on screen 2
Or mby there is a way to insta print marked columns without creating word/pdf ?
The best approach I can think is to make an HTML page with CSS that is prepared to be printed as labels and using the browser for printing.
This means making a template and using HtmlService.createTemplateFromFile (see docs) to generate a template, which then is evaluated into a web page. Then using Ui.showModelessDialog (see docs) you show a dialog that can call window.print (see MDN reference) so you can print the contents of the iframe that contains the generated labels.
function printLabels(labels) {
const labelsPage = HtmlService.createTemplateFromFile('Labels');
labelsPage.labels = labels;
SpreadsheetApp.getUi().showModelessDialog(labelsPage.evaluate(), 'Labels');
}
Note that we are using the file Labels. Using another name won't work.
Now we have to make the template (templated HTML docs) of the labels using HTML and CSS which should be designed with printing in mind. Also we need to use JavaScript to trigger the browser's print dialog.
Since printing is hard to implement correctly and debug, and I was motivated enough, I took the time to make a version of something similar that what you have right now. Obviously you'll probably have to change it a bit. I took the sizes from your script but I don't have a label printer —and obviously not your printer— so you'll have to make some tests to ensure that everything works as it should.
<!DOCTYPE html>
<html>
<head>
<!-- Basic CSS -->
<style type="text/css">
:root {
font-family: Helvetica, sans-serif;
}
:root, html, body {
margin: 0;
padding: 0;
}
article {
padding: 5pt;
}
header {
margin: 0;
line-height: 1em;
display: flex;
justify-content: space-between;
font-size: 13pt;
}
p {
margin: 0.7em;
padding: 0;
font-size: 10pt;
}
</style>
<!-- CSS to setup the printing -->
<style type="text/css" media="print">
#page {
/* Define the size of the page (label) */
size: 260pt 100pt;
/* Remove the page information that the browser usually adds */
margin: 0;
marks: crop;
}
body > * {
/* Make sure there is 1 article per page (label) */
page-break-after: always;
}
button {
/* Hide the print button when actually printing */
display: none;
}
</style>
<!-- Script that opens the print dialog after the page has completly loaded :) -->
<script>
// Show the printing dialog
window.addEventListener('load', function() {
window.print();
})
// Close after printing
window.addEventListener('afterprint', function() {
google.script.host.close();
});
</script>
</head>
<body>
<button type="button" onclick="window.print()">Print</button>
<? for (let label of labels) { ?>
<article>
<header>
<div><?= label[0] ?></div>
<div><?= label[1] ?>mm</div>
</header>
<p><?= label[2] ?></p>
<p><?= label[3] ?></p>
</article>
<? } ?>
</body>
</html>
Now you can call printLabels with an array of labels to print (in your case the data variable should work). Notice that this will print all the labels given, so if you'd like only print 4, you'should only send 4 to that function.
References and further reading
CSS media queries (MDN)
CSS flexbox (MDN)
CSS units (MDN)
CSS #page (MDN)
Can I remove the URL from my print css, so the web address doesn't print? (StackOverflow)
google.script.host.close() (Google Apps Script reference)

Export html from draft js editor and keeping the style

I am using draft js to create email templates in a reactJs application.
I implemented custom block types and with css I was able to align my columns properly (left, center, right). I used RichUtils to toggle block type.
However, my problem is when I am exporting the editor state into html, only the tags are exported, but I need the style too, so that the text-align style remains the same.
I use stateToHtml from draft-js-export-html when exporting the html.
I was also thinking about adding custom attributes, but I was not successful with it yet.
I appreciate every answer and thank you for the help in advance.
you can try this way:
import { ContentState, convertToRaw } from 'draft-js';
import draftToHtml from 'draftjs-to-html';
const currentContent = this.state.editorState.getCurrentContent();
return draftToHtml(convertToRaw(currentContent));
As an extension on #ArtemZubarev , there will be a problem when exporting it to html because it will contain no styles. So this requires 2 answers
How to get state to html: credits to #ArtemZubarev
import { ContentState, convertToRaw } from 'draft-js';
import draftToHtml from 'draftjs-to-html';
const currentContent = this.state.editorState.getCurrentContent();
return draftToHtml(convertToRaw(currentContent));
However, this will return unstyled elements, for example: <h1>Hello World</h1>.
This raises the question: How to keep the styling?
Option 1: CSS
Option 2: Create a render function that will inject the styles as inline
private injectHTML = (html?: string) => {
return `<!DOCTYPE html>
<html>
<head>
<style>
body {background-color: powderblue;}
h1 {color: blue;}
p {color: red;}
</style>
</head>
<body>
${
html
}
</body>
</html>`
}

Typescript Angular 5 add dynamically css to component

I have a really special issue. I am using AngularTS with C# on serverside and Typescript on clientside.
I want to add to our application the possibility, that the customer can add css styles in a textbox or add file locations (external).
In a custom component I want to add the html code from the official site of the customer. The customer should add the css files or text in order to show the content on our page like the content of the official site.
Unfortunately I could not find a possibility to add the css files.
#Component({
selector: 'customer-footer',
templateUrl: './customer-footer.component.html'
styleUrls: getCSS()})
export function getCSS(): string [] {
var styles: string[] = [];
styles.push("https://");
return styles;}
This doesn't work because AOT is not compatible with static refs.
I tried to add the css Content in styles Tag (Body-Area):
<style type="text/css">
{{footerCSS}}</style>
My best result was to add a Object (css) in ngStyle to the displaying .
<div *ngIf="!fixedBottom"
[innerHtml]="footerHtml"
[ngStyle]="footerCSS"></div>
Unfortunately I could not find a way to convert css code like this one:
.flatWeatherPlugin {
font-size: inherit;
width: 100%;
}
.flatWeatherPlugin p, .flatWeatherPlugin h2, .flatWeatherPlugin h3, .flatWeatherPlugin ul, .flatWeatherPlugin li {
padding: 0;
margin: 0;
color: inherit;
}
To a functional object. Has someone an idea or a helpful message?
In typescript you can use this to load css dynamically
require('css/stylesheet.css')

How to use 'target' css psuedo-class on html 'name' attribute

I'm trying to use the ":target" CSS class to highlight a section of html based on a link clicked that includes an anchor fragment(ex: C:\Desktop\Test.html#link). The regions that are being modified in my document have "name" identity attributes. The target pseudo class worked with "id" attributes for me but am having trouble with "name". Thanks.
PS: The reason I'm using "name" is because I'm writing VBA scripts about HTML documents that were directly converted from MS Word. (Word uses "name" for bookmark conversions to links)
Sample Code I have tried:
a:target {
color: red;
}
a[name = test]:target {
color: red;
}
1st CSS is just for styling (how the content looks, layout etc...), use js you can easily update the content (check the example)
CSS describes how HTML elements are to be displayed on screen, paper, or in other media.
2nd
If you just want to check if the <a> element has a name attribute, then use a[name] (2nd link in my example)
If you need partial match do a[name*=test], any name contains test will be selected. (3rd link in my example)
var alltest = document.getElementsByName('test');
alltest.forEach(function(test) {
test.setAttribute('href', '#newlink');
test.innerHTML = 'updated link';
});
a[name] {
color: green;
}
a[name*=test] {
color: red;
}
google.com<br>
<a name="alsowork" href="google.com">google.com</a><br>
<a name="test" href="google.com">google.com</a>
<a name="test" href="google.com">google.com</a>
<a name="test" href="google.com">google.com</a>

Remove Certain CSS Style from Html Page

I have a Html page which has anchor tag, I Need to remove certain style applied already in html page for anchor tag while the html page is opened throw Iframe.
HTML Content as below:
<html>
<body>
<div>some content<a href="http://www.website.com" name="test1"/> some content </div>
</body>
</html>
I tried as below:
a[name^="test1"]:before{
content:"[prefix text]";
display:inline;
color:red;
}
a[name^="test1"]:after{
content:"suffix text";
display:inline;
color:green;
}
iframe a[name^="test1"]:before{
display:none;
}
iframe a[name^="test1"]:after{
display:none;
}
But inside "iframe" also these styles has been applying.
You have to first detect if your page is rendered inside an iframe and in that case apply an alternative CSS. It' can't be done with vanilla CSS then it has to be done with some JavaScript:
<script type="text/javascript">
function getTopWindow() {
try {
return window.top;
} catch {
// If we can't access window.top then browser is restricting
// us because of same origin policy.
return true;
}
}
function isRendererdInFrame() {
// If top window is null we may safely assume we're in iframe
return window.self !== getTopWindow();
}
function loadCss(location) {
if(document.createStyleSheet) {
document.createStyleSheet('http://server/stylesheet.css');
} else {
var styles = "#import url('" + location + "');";
var newSS=document.createElement('link');
newSS.rel='stylesheet';
newSS.href='data:text/css,'+escape(styles);
document.getElementsByTagName("head")[0].appendChild(newSS);
}
}
</script>
Code to load CSS from JavaScript is from How to load up CSS files using Javascript?.
With all that code you may simply write (even just after that inside <script> block):
var cssToLoad = isRendererdInFrame() ? "iframe.css" : "not-iframe.css";
loadCss("http://server/" + cssToLoad);
Of course same technique can be applied to patch CSS with iframe specific styles:
if (isRenderedInFrame())
loadCss("http://server/iframe-patch.css");
i dont know how to detect if page is opened in iframe or not, but there is one possible(not very nice) workaround, you can set iframe to width which is not commonly used by devices (example 463px) and then set media query for this resolution which apply when content is shown in this iframe. This is really nasty way since its not 100% and i would not recommending that.