質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Google Apps Script

Google Apps ScriptはGoogleの製品と第三者のサービスでタスクを自動化するためのJavaScriptのクラウドのスクリプト言語です。

Q&A

解決済

1回答

2470閲覧

GASで高さに合わせたりページに合わせたPDFを出力する

t.hiro0120

総合スコア3

Google Apps Script

Google Apps ScriptはGoogleの製品と第三者のサービスでタスクを自動化するためのJavaScriptのクラウドのスクリプト言語です。

0グッド

1クリップ

投稿2023/04/27 02:15

編集2023/04/27 02:19

実現したいこと

GASでPDF出力する際に高さに合わせたりページに合わせたりして出力したい

###前提

GASに触り始めて一週間経たない位の者です。
自分が調べた記事や質問では、GASのPDF出力は幅に合わせるか否かしか指定できず高さに合わせる等の指定は出来ませんでした。(下記に使ったオプションを記します)

GAS

1let pdfOptions = "&exportFormat=pdf&format=pdf" 2 + "&size=A4" //用紙サイズ 3 + "&portrait=true" //用紙の向き true: 縦向き / false: 横向き 4 + "&fitw=true" //ページ幅を用紙にフィットさせるか true: フィットさせる / false: 原寸大 5 + "&top_margin=0.50" //上の余白 6 + "&right_margin=0.50" //右の余白 7 + "&bottom_margin=0.50" //下の余白 8 + "&left_margin=0.50" //左の余白 9 + "&horizontal_alignment=CENTER" //水平方向の位置 10 + "&vertical_alignment=TOP" //垂直方向の位置 11 + "&printtitle=false" //スプレッドシート名の表示有無 12 + "&sheetnames=false" //シート名の表示有無 13 + "&gridlines=false" //グリッドラインの表示有無 14 + "&fzr=false" //固定行の表示有無 15 + "&fzc=false" //固定列の表示有無;

調べていたらエクスポート画面をエミュレートする形でPDFを出力するコードがありました。
こちらの質問で回答としてこちらのサイトを参考にしたコードがあります。

発生している問題・エラーメッセージ

まず上記の質問に対する回答で書かれたコードを実行するとhtmlのエラーメッセージが生成されPDFとして保存されます。
以下のエラー文が出力されます。

html

1Error: (500) 2<!DOCTYPE html> 3 <html lang="ja"> 4 <head> 5 <meta name="description" content="ウェブ ワープロ、プレゼンテーション、スプレッドシート"> 6 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0"> 7 <link rel="shortcut icon" href="//docs.google.com/favicon.ico"> 8 <title> 9 エラー 10 </title> 11 <meta name="referrer" content="origin"> 12 <link href="//fonts.googleapis.com/css?family=Product+Sans" rel="stylesheet" type="text/css" nonce="pDkiWRswUeRmjvBN0Pfzzg"> 13 <style nonce="pDkiWRswUeRmjvBN0Pfzzg"> 14 .goog-inline-block{position:relative;display:-moz-inline-box;display:inline-block} 15 * html .goog-inline-block{display:inline} 16 *:first-child+html .goog-inline-block{display:inline} 17 #drive-logo{margin:18px 0;position:absolute;white-space:nowrap} 18 .docs-drivelogo-img{background-image:url(//ssl.gstatic.com/images/branding/googlelogo/1x/googlelogo_color_116x41dp.png);-webkit-background-size:116px 41px;-moz-background-size:116px 41px;background-size:116px 41px;display:inline-block;height:41px;vertical-align:bottom;width:116px} 19 .docs-drivelogo-text{color:#000;display:inline-block;opacity:.54;text-decoration:none;font-family:"Product Sans",Arial,Helvetica,sans-serif;font-size:32px;text-rendering:optimizeLegibility;position:relative;top:-6px;left:-7px;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale} 20 @media (-o-min-device-pixel-ratio:3/2),(-webkit-min-device-pixel-ratio:1.5),(min--moz-device-pixel-ratio:1.5),(min-resolution:144dpi){.docs-drivelogo-img{background-image:url(//ssl.gstatic.com/images/branding/googlelogo/2x/googlelogo_color_116x41dp.png)}} 21 sentinel{} 22 </style> 23 <style type="text/css" nonce="pDkiWRswUeRmjvBN0Pfzzg"> 24 body {background-color: #fff; font-family: Arial,sans-serif; font-size: 13px; margin: 0; padding: 0;}a, 25 a:link, 26 a:visited {color: #112ABB;} 27 </style> 28 <style type="text/css" nonce="pDkiWRswUeRmjvBN0Pfzzg"> 29 .errorMessage {font-size: 12pt; font-weight: bold; line-height: 150%;} 30 </style> 31 </head> 32 <body> 33 <div id="outerContainer"> 34 <div id="innerContainer"> 35 <div style="position: absolute; top: -80px;"> 36 <div style="margin: 18px 0; position: absolute; white-space: nowrap;"> 37 <a href="//support.google.com/docs/"> 38 <img height="35px" src="//ssl.gstatic.com/docs/common/product/spreadsheets_lockup2.png" alt="Google ロゴ"/> 39 </a> 40 </div> 41 </div> 42 <p style="padding-top: 15px"> 43 Google ドキュメント内でエラーが発生しました。このページを再読み込みするか、数分後にこのページをもう一度開いてみてください。 44 </p> 45 <p> 46 Google ドキュメント エディタの詳細については、 47 <a href="https://support.google.com/docs/?hl=ja&p=error_help" target="_blank"> 48 ヘルプセンター 49 </a> 50 をご参照ください。 51 </p> 52 <p> 53 <br> 54 <b> 55 ご不便をおかけして申し訳ございません。 56 </b> 57 <br> 58 <i> 59 - Google Docs チーム 60 </i> 61 </p> 62 </div> 63 </div> 64 </body> 65 <style nonce="pDkiWRswUeRmjvBN0Pfzzg"> 66 html {height: 100%; overflow: auto;}body {height: 100%; overflow: auto;}#outerContainer {margin: auto; max-width: 750px;}#innerContainer {margin-bottom: 20px; margin-left: 40px; margin-right: 40px; margin-top: 80px; position: relative;} 67 </style> 68</html>

ロシア語で書かれたサイトを見ると、Operaのデベロッパーツールのネットワークタブで pdf?1d= から始まるリンクの中身を見てコードを書いている様なのですが自分がスプレッドシートの印刷からPDF出力しても pdf?1d= から始まるリンクがありませんでした(画像はOperaのデベロッパーツール)イメージ説明

該当のソースコード

GAS

1function encodeDate(yy, mm, dd, hh, ii, ss) { 2 var days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; 3 if (yy % 4 == 0 && (yy % 100 != 0 || yy % 400 == 0)) days[1] = 29; 4 for (var i = 0; i < mm; i++) dd += days[i]; 5 yy--; 6 return ( 7 ((((yy * 365 + 8 (yy - (yy % 4)) / 4 - 9 (yy - (yy % 100)) / 100 + 10 (yy - (yy % 400)) / 400 + 11 dd - 12 693594) * 13 24 + 14 hh) * 15 60 + 16 ii) * 17 60 + 18 ss) / 19 86400.0 20 ); 21} 22 23function exportPDF() { 24 const file = SpreadsheetApp.getActiveSpreadsheet(); 25 const sheet = file.getSheets()[0]; 26 27 const lastRow = sheet.getLastRow(); 28 const lastCol = sheet.getLastColumn(); 29 30 const exportSource = [ 31 [ 32 sheet.getSheetId().toString(), //シートID 33 0, // 範囲:開始行(0~) 34 lastRow, // 範囲:終了行 35 0, // 範囲:開始列(0~) 36 lastCol, // 範囲:終了列 37 ], 38 ]; 39 const exportOptions = [ 40 0, // 印刷形式:メモを表示 0:なし 1:あり 41 null, // (不明) 42 1, // 印刷形式:グリッド線を表示 0:あり 1:なし 43 0, // ヘッダーとフッター:ページ番号 0:なし 1:あり 44 0, // ヘッダーとフッター:ワークブックのタイトル 0:なし 1:あり 45 0, // ヘッダーとフッター:シート名 0:なし 1:あり 46 0, // ヘッダーとフッター:現在の日付 0:なし 1:あり 47 0, // ヘッダーとフッター:現在の時刻 0:なし 1:あり 48 1, // 行と列の見出し:固定行を繰り返す 0:なし 1:あり 49 1, // 行と列の見出し:固定列を繰り返す 0:なし 1:あり 50 2, // ページの印刷順序 1:上から下 2:左から右 51 1, // (不明) 52 null, // (不明) 53 null, // (不明) 54 1, // 配置:水平 1:左 2:中央 3:右 55 1, // 配置:垂直 1:上 2:中央 3:下 56 ]; 57 const exportFormat = [ 58 "A4", // 用紙サイズ 59 0, // ページの向き 0:横向き 1:縦向き 60 2, // スケール 0:標準(100%) 1:幅に合わせる 2:高さに合わせる 3:ページに合わせる 4:カスタム 61 1, // スケールで4(カスタム)選択時のパーセント(0~1の範囲) 62 [ 63 0.75, // 上部オフセット 0.75インチ 64 0.75, // 下部オフセット 0.75インチ 65 0.7, // 左オフセット 0.7インチ 66 0.7, // 右オフセット 0.7インチ 67 ], 68 ]; 69 70 var today = new Date(); 71 var d = encodeDate( 72 today.getFullYear(), 73 today.getMonth(), 74 today.getDate(), 75 today.getHours(), 76 today.getMinutes(), 77 today.getSeconds() 78 ); 79 var pc = [ 80 null, 81 null, 82 null, 83 null, 84 null, 85 null, 86 null, 87 null, 88 null, 89 0, 90 exportSource, 91 10000000, 92 null, 93 null, 94 null, 95 null, 96 null, 97 null, 98 null, 99 null, 100 null, 101 null, 102 null, 103 null, 104 null, 105 null, 106 d, 107 null, 108 null, 109 exportOptions, 110 exportFormat, 111 null, 112 0, 113 null, 114 0, 115 ]; 116 117 const ssID = file.getId(); 118 119 const options = { 120 method: "post", 121 payload: "a=true&pc=" + JSON.stringify(pc) + "&gf=[]", 122 headers: { Authorization: "Bearer " + ScriptApp.getOAuthToken() }, 123 muteHttpExceptions: true, 124 }; 125 126 const res = UrlFetchApp.fetch( 127 "https://docs.google.com/spreadsheets/d/" + ssID + "/pdf?id=" + ssID, 128 options 129 ); 130 131 const resCode = res.getResponseCode(); 132 if (resCode !== 200) { 133 throw new Error(`(${resCode}) ${res.getContentText()}`); 134 } 135 136 const blob = res.getBlob(); 137 const filename = file.getName() + " " + sheet.getName() + ".pdf" 138 DriveApp.createFile(blob).setName(filename); 139}

試したこと

上記ソースコードのresCodeをconsole.log()で表示したところ500が表示される。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

YellowGreen

2023/04/27 02:59

scale = x x = 1: 標準100% 2: 幅に合わせる 3: 高さに合わせる 4: ページに合わせる で設定できませんか?
t.hiro0120

2023/04/27 04:31

その方法で指定できました! ありがとうございます!
guest

回答1

0

自己解決

コメントの方でGASのpdfオプションでスケールの指定をする方法を教えてくださった方が居たので解決しました!

下記のscaleで指定できました

GAS

1let pdfOptions = "&exportFormat=pdf&format=pdf" 2 + "&size="+size //用紙サイズ (A4) 3 + "&portrait="+portrait //用紙の向き true: 縦向き / false: 横向き 4 + "&scale="+scale //1: 標準100% 2: 幅に合わせる 3: 高さに合わせる 4: ページに合わせる 5 + "&top_margin=0.50" //上の余白 6 + "&right_margin=0.50" //右の余白 7 + "&bottom_margin=0.50" //下の余白 8 + "&left_margin=0.50" //左の余白 9 + "&horizontal_alignment=CENTER" //水平方向の位置 10 + "&vertical_alignment=TOP" //垂直方向の位置 11 + "&printtitle=false" //スプレッドシート名の表示有無 12 + "&sheetnames=false" //シート名の表示有無 13 + "&gridlines=false" //グリッドラインの表示有無 14 + "&fzr=false" //固定行の表示有無 15 + "&fzc=false" //固定列の表示有無 16 + "&printnotes=false" //コメントの表示有無;

投稿2023/04/27 04:35

t.hiro0120

総合スコア3

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問