使用Google Apps脚本将HTML网络应用表格转换为PDF

时间:2017-07-13 09:39:32

标签: google-apps-script

每一个我都有HTML网络应用程序,我已经将电子表格转换为HTML表格现在我想将此HTML网络应用程序表格转换为PDF格式。如何实现这一目标?

这是我的代码

的index.html

  <html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", '1', {packages:['table']});
google.setOnLoadCallback(drawTable);
   function drawTable() {
   var query = new google.visualization.Query(
     'https://docs.google.com/spreadsheets/d/1w5kqFmt1yclKVDdasujf-
  moxjX3QOVsyDyP7DIcF2u0/edit#gid=0');

  query.send(handleQueryResponse);
    }

  function handleQueryResponse(response) {
   if (response.isError()) {
     alert('Error in query: ' + response.getMessage() + ' ' + 
  response.getDetailedMessage());
     return;
 }

    var data = response.getDataTable();
  var table = new 
  google.visualization.Table(document.getElementById('table_div'));
 var options = {'title':'Bass Naming',
                  'width':'600',
                  'height':'400'};


   table.draw(data, options);
   var tabUri = table.getLinkUrl();

    }  

  </script>

  <title>Data from a Spreadsheet</title>
   </head>

  <body>
   <div>My Sheet</div>
  <div id="table_div"></div>
 <div>
  <h3> click here the below button to send the Table as PDF to mail :</h3>
   <button id='mail' onclick="myFunction()">Send to Mail</button>  
  </div>
 </body>
</html>

code.gs

 function doGet(e) {
   return HtmlService
  .createTemplateFromFile("index")
   .evaluate()
  .setTitle("Charts for Rent Analyser")
  .setSandboxMode(HtmlService.SandboxMode.IFRAME);
}

This is my Screen Short of my current Page

1 个答案:

答案 0 :(得分:0)

在code.gs中添加以下函数,将表格邮寄为PDF附件。

* {
	box-sizing: border-box;
}

body {
	background-color:#f6f6f6;
	background-image: url("img/bg.png");
	background-repeat: no-repeat;
	background-attachment: fixed;
	background-position: center center;
	background-size:cover;
	overflow-x:hidden;
	overflow-y:scroll;
	margin:0;
	padding:0!important;
}

.navbar {
    position: relative;
	border:1px solid transparent;
	min-height: 80px;
	border-top: 0;
	border-bottom:1px solid #e7e7e7;
	margin-bottom:0;
	z-index: 100;
}

.navbar-default {
  height: 80px;
}

.navbar-brand {
    float:left;
    font-size:18px;
    line-height:80px;
    height:80px;
    padding:0 15px;
}

.navbar-toggle {
	margin-top: 23px;
	padding: 9px 10px !important;
}


.navbar-nav > li > a {
	height: 80px;
	padding-top:10px;
    padding-bottom:10px;
    line-height:20px;
	border-left: 1px solid #e7e7e7;
}

.navbar-left {
    float:left!important;
}

.navbar-right {
    float:right!important;
    margin-right:-15px;
}

.navbar-nav>li>.dropdown-menu {
    margin-top:0;
    border-top-right-radius:0;
    border-top-left-radius:0;
}

另外,请确保从“高级Google服务”启用云端硬盘服务,并从“ Goolge API控制台”启用云端硬盘API。

有关详细代码,请参阅以下链接。

Google App Script

Exec Link