我在Google表格中有文件(img 1)。
https://i.stack.imgur.com/1j8ny.png [1]
我写了将标记的列转换为单词的脚本(我只需要标记第1到4列,因为它要贴在贴纸上)(img 2)
https://i.stack.imgur.com/GnIXo.png [2]
这是代码:
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);
}
}
}
有什么选择可以使用打印机从Google表格脚本编辑器中打印Word文档?
如果我在脚本中标记了4列,它将向我打印标签。我正在使用打印机“ Zebra GK420t”
我将其转换为Word文档,因为当我将其转换为pdf时,我无法将页面大小更改为高度100和重量260,而我现在不知道如何将类似的内容放置在屏幕2上
还是mby有一种方法可以在不创建word / pdf的情况下打印标记的列?
我能想到的最好方法是使用CSS制作HTML页面,准备将其打印为标签并使用浏览器进行打印。这意味着制作一个模板,并使用HtmlService.createTemplateFromFile
(请参阅docs)生成一个模板,然后将其评估为网页。然后使用Ui.showModelessDialog
(请参阅docs)显示一个可以调用的对话框window.print
(请参阅MDN参考),以便可以打印iframe
包含所生成标签的内容。
function printLabels(labels) {
const labelsPage = HtmlService.createTemplateFromFile('Labels');
labelsPage.labels = labels;
SpreadsheetApp.getUi().showModelessDialog(labelsPage.evaluate(), 'Labels');
}
请注意,我们正在使用该文件Labels
。使用其他名称将无效。
现在,我们必须使用HTML和CSS来制作标签的模板(模板化的HTML文档),并且在设计时应考虑打印。另外,我们需要使用JavaScript触发浏览器的打印对话框。
由于打印难以正确实施和调试,并且我有足够的动力,因此我花了一些时间来制作与你现在所拥有的相似的版本。显然,你可能需要对其进行一些更改。我从你的脚本中获取了尺寸,但是我没有标签打印机,而且显然没有你的打印机,因此你必须进行一些测试以确保一切正常。
<!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>
现在,你可以调用printLabels
一个带有标签的数组来进行打印(在你的情况下,该data
变量应该起作用)。请注意,这将打印给定的所有标签,因此,如果你只想打印4,则只应向该函数发送4。
非常感谢您的宝贵时间!我会试试的 :)
@Kubuniunio如果可以的话,出于文档目的,请接受对您有所帮助的答案(✓)-它将帮助以后遇到相同问题的其他人也找到解决方案:)