Xzi*_*ila 3 mailto gmail google-sheets google-apps-script
我有Excel电子表格来编写时间表并将它们发送给工作人员.这涉及vba,它隐藏列并保存为PDF.
为了让我使用Google-Sheets打印到PDF,在Gmail中打开单个电子邮件似乎效率较低.我发现你可以复制(ctrl + c)一个范围(例如:"A1:E10"),然后直接粘贴到Gmail(ctrl + v)中它看起来一样好.
我想要做的是按一个按钮来运行一个脚本:
要么
要么
看到这里(这个'表'的公开版本)

我是Google Scripts的新手,但熟悉VBA(以及一般的面向对象编程,脚本语言XD除外)
任何帮助或来源,或完成相同的事情的替代解决方案将是非常有帮助的.
Mog*_*dad 12
由于Google表格不是计算机上运行的应用程序,因此其脚本功能与Excel中的VBA非常不同.例如,无法访问PC的剪贴板.没有触发打印对话框,另一个.您可以在使用表格时在浏览器中执行这些操作,但不能从脚本中执行.
但是,考虑到Google Apps脚本的功能,最直接的方法是:
没有必要以这种方式隐藏或取消隐藏列,因为嵌入式计划只能由有趣的列构建.
你已经要求保留格式和范围值,所以这里有一种方法可以做到这一点.的sendMail()功能上活跃的电子表格操作,并且读取从一个固定的范围的调度对片材,构建一个电子邮件,并将其发送到对片发现的电子邮件地址.
有关最新代码,请参阅Github中的此库.
function sendEmail() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var recipient = sheet.getRange("I4").getValue(); // "TO" email address
var subject = Utilities.formatDate(
sheet.getRange("E2").getValue(),
ss.getSpreadsheetTimeZone(),
"MMM d EEE");
var schedRange = sheet.getRange("B5:G26");
// Put Name & Date into email first.
// We only want the schedule within borders, so
// these are handled separately.
var body = '<div style="text-align:center;display: inline-block;font-family: arial,sans,sans-serif">'
body += '<H1>'+ sheet.getRange("E1").getValue() +'</H1>';
body += '<H2>'
+ Utilities.formatDate(
sheet.getRange("E2").getValue(),
ss.getSpreadsheetTimeZone(),
"EEEEE, MMMMM d, yyyy")
+ '</H2>';
body += getHtmlTable(schedRange);
body += '</div>';
debugger;
recipient = Session.getActiveUser().getEmail(); // For debugging, send only to self
GmailApp.sendEmail(recipient, subject, "Requires HTML", {htmlBody:body})
}
Run Code Online (Sandbox Code Playgroud)
该sendEmail()函数依赖于getHtmlTable(),这是将电子表格范围呈现为HTML表的常规实用程序的开始.有关最新版本,请参阅github.
注意事项:
tableFormat变量中设置.由于无法确定电子表格中的边框,因此无法传输它们.码:
/**
* Return a string containing an HTML table representation
* of the given range, preserving style settings.
*/
function getHtmlTable(range){
var ss = range.getSheet().getParent();
var sheet = range.getSheet();
startRow = range.getRow();
startCol = range.getColumn();
lastRow = range.getLastRow();
lastCol = range.getLastColumn();
// Read table contents
var data = range.getValues();
// Get css style attributes from range
var fontColors = range.getFontColors();
var backgrounds = range.getBackgrounds();
var fontFamilies = range.getFontFamilies();
var fontSizes = range.getFontSizes();
var fontLines = range.getFontLines();
var fontWeights = range.getFontWeights();
var horizontalAlignments = range.getHorizontalAlignments();
var verticalAlignments = range.getVerticalAlignments();
// Get column widths in pixels
var colWidths = [];
for (var col=startCol; col<=lastCol; col++) {
colWidths.push(sheet.getColumnWidth(col));
}
// Get Row heights in pixels
var rowHeights = [];
for (var row=startRow; row<=lastRow; row++) {
rowHeights.push(sheet.getRowHeight(row));
}
// Future consideration...
var numberFormats = range.getNumberFormats();
// Build HTML Table, with inline styling for each cell
var tableFormat = 'style="border:1px solid black;border-collapse:collapse;text-align:center" border = 1 cellpadding = 5';
var html = ['<table '+tableFormat+'>'];
// Column widths appear outside of table rows
for (col=0;col<colWidths.length;col++) {
html.push('<col width="'+colWidths[col]+'">')
}
// Populate rows
for (row=0;row<data.length;row++) {
html.push('<tr height="'+rowHeights[row]+'">');
for (col=0;col<data[row].length;col++) {
// Get formatted data
var cellText = data[row][col];
if (cellText instanceof Date) {
cellText = Utilities.formatDate(
cellText,
ss.getSpreadsheetTimeZone(),
'MMM/d EEE');
}
var style = 'style="'
+ 'color: ' + fontColors[row][col]+'; '
+ 'font-family: ' + fontFamilies[row][col]+'; '
+ 'font-size: ' + fontSizes[row][col]+'; '
+ 'font-weight: ' + fontWeights[row][col]+'; '
+ 'background-color: ' + backgrounds[row][col]+'; '
+ 'text-align: ' + horizontalAlignments[row][col]+'; '
+ 'vertical-align: ' + verticalAlignments[row][col]+'; '
+'"';
html.push('<td ' + style + '>'
+cellText
+'</td>');
}
html.push('</tr>');
}
html.push('</table>');
return html.join('');
}
Run Code Online (Sandbox Code Playgroud)
PS:我认为彩色网格是一个火狐怪.在Chrome中看起来很好,HTML确实指定了黑色.

| 归档时间: |
|
| 查看次数: |
6198 次 |
| 最近记录: |