-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.gs
More file actions
1294 lines (1143 loc) · 55.1 KB
/
Copy pathCode.gs
File metadata and controls
1294 lines (1143 loc) · 55.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* 請求書自動化システム — メイン処理
*
* 機能:
* - Web App として申請者用フォームを提供(doGet)
* - フォーム送信を受け取り Sheet に保存(submitInvoiceRequest)
* - 管理者が承認 → PDF 生成 → メール送付(generateInvoiceForRow)
* - 一括処理用メニュー(onOpen)
*/
const WITHHOLDING_THRESHOLD = 1000000;
/* ──────────────────── Web App エントリーポイント ──────────────────── */
function doGet(e) {
const requestedLang = (e && e.parameter && e.parameter.lang) || '';
const settings = getSettings_();
const companyNames = {
'ja-JP': settings['company_name_ja-JP'] || '',
'en-US': settings['company_name_en-US'] || '',
'zh-TW': settings['company_name_zh-TW'] || '',
'es-ES': settings['company_name_es-ES'] || ''
};
const supportedLangs = Object.keys(companyNames);
// BCP 47 locale を解決する: exact match → prefix match → デフォルト ja-JP
let lang = 'ja-JP';
if (supportedLangs.indexOf(requestedLang) >= 0) {
lang = requestedLang;
} else if (requestedLang) {
const prefix = requestedLang.split('-')[0];
const matched = supportedLangs.find(l => l === prefix || l.indexOf(prefix + '-') === 0);
if (matched) lang = matched;
}
const t = HtmlService.createTemplateFromFile('index');
t.lang = lang;
t.companyNames = companyNames;
const titleName = companyNames['ja-JP'] || companyNames['en-US'] || 'Invoice';
return t.evaluate()
.setTitle(titleName + ' 請求書発行')
.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL)
.addMetaTag('viewport', 'width=device-width, initial-scale=1');
}
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename).getContent();
}
/* ──────────────────── フォーム送信ハンドラ ──────────────────── */
/**
* Web App フォームから呼ばれる
* @param {Object} payload - フォームデータ
* @returns {Object} { ok, message }
*/
function submitInvoiceRequest(payload) {
try {
const validated = validatePayload_(payload);
const ss = getMainSpreadsheet_();
const sheet = ss.getSheetByName(SHEET_NAMES.SUBMISSIONS);
const defaultWithholding = validated.residence === 'overseas' ? 'N/A' : 'no';
const rowData = {
'審査ステータス': 'pending',
'源泉適用': defaultWithholding,
'タイムスタンプ': new Date(),
'言語': validated.language,
'居住地': validated.residence,
'氏名': validated.name,
'住所': validated.address,
'電話番号': validated.phone,
'メールアドレス': validated.email,
'振込銀行名': validated.bankName,
'支店名': validated.branchName,
'口座名義': validated.accountName,
'口座番号': validated.accountNumber,
'通貨': validated.currency,
'請求明細(JSON)': JSON.stringify(validated.items),
'備考': validated.notes,
'請求書番号': '',
'PDF生成日': '',
'PDF URL': '',
'メモ': '自動生成'
};
const row = SUBMISSION_COLUMNS.map(col => rowData[col] !== undefined ? rowData[col] : '');
sheet.appendRow(row);
const rowNumber = sheet.getLastRow();
// 電話番号・口座番号は先頭の「0」が数値化で消えないよう、必ず文字列として保存し直す。
// (appendRow はセル書式により "0987..." を数値 987... に丸めてしまうため)
forceTextCells_(sheet, rowNumber, {
'電話番号': validated.phone,
'口座番号': validated.accountNumber
});
try {
generateInvoiceForRow(rowNumber);
} catch (pdfErr) {
Logger.log('Auto PDF generation failed: ' + pdfErr.stack);
notifyAdminOfPdfFailure_(validated, rowNumber, pdfErr);
}
return { ok: true, message: 'submitted' };
} catch (err) {
Logger.log('submitInvoiceRequest error: ' + err.stack);
return { ok: false, message: String(err.message || err) };
}
}
/**
* 指定行の指定カラムを「書式: プレーンテキスト(@)」で保存し直す。
* 先頭ゼロを保持したい電話番号・口座番号などに使う(数値化で 0 が消えるのを防ぐ)。
* @param {Sheet} sheet
* @param {number} rowNumber
* @param {Object} colNameToValue - { カラム名: 文字列値 }
*/
function forceTextCells_(sheet, rowNumber, colNameToValue) {
Object.keys(colNameToValue).forEach(colName => {
const idx = SUBMISSION_COLUMNS.indexOf(colName);
if (idx < 0) return;
const value = colNameToValue[colName];
const cell = sheet.getRange(rowNumber, idx + 1);
cell.setNumberFormat('@');
cell.setValue(String(value == null ? '' : value));
});
}
/**
* 【一回限りのメンテナンス】既存データの電話番号・口座番号を修復する。
* エディタから関数を選んで手動実行する(末尾アンダースコアなし=実行ドロップダウンに表示)。
*
* - 数式化セル(「+」始まりの国際電話番号・口座番号): テキスト書式が無かった頃に
* "+819…" 等が数式として取り込まれ #ERROR 化している。getFormula() で元の入力文字列を
* 取得し、先頭の "=" を外して文字列として復元する(「+」を正しく取り戻す)。
* - 電話番号: 数値で保存されている = 先頭ゼロが落ちている状態。先頭に "0" を補って文字列化する。
* (日本・台湾の電話番号は必ず 0 始まりのため、数値化で落ちた 1 桁を安全に復元できる)
* - 口座番号: 数値なら文字列化のみ。落ちたゼロの桁数は特定できないため自動補完はせず、
* 要確認リストとしてログに出す。
* - 両列をプレーンテキスト書式に設定し、今後の手入力・自動保存でもゼロが消えないようにする。
*
* 冪等: 既に文字列のセルは触らない。何度実行しても安全。変更内容は全てログに残す。
*/
function fixExistingContactData() {
const ss = getMainSpreadsheet_();
const sheet = ss.getSheetByName(SHEET_NAMES.SUBMISSIONS);
if (!sheet) throw new Error('申請データシートが見つかりません');
const phoneCol = SUBMISSION_COLUMNS.indexOf('電話番号') + 1;
const acctCol = SUBMISSION_COLUMNS.indexOf('口座番号') + 1;
// 今後の入力でもゼロが消えないよう、列をテキスト書式にする
if (sheet.getMaxRows() > 1) {
sheet.getRange(2, phoneCol, sheet.getMaxRows() - 1, 1).setNumberFormat('@');
sheet.getRange(2, acctCol, sheet.getMaxRows() - 1, 1).setNumberFormat('@');
}
const lastRow = sheet.getLastRow();
if (lastRow < 2) {
Logger.log('データ行がありません。列の書式のみ設定しました。');
return 'no-data';
}
const phoneFixes = [];
const acctReview = [];
const formulaFixes = [];
// 「+」始まりの国際電話番号・口座番号は、テキスト書式が無かった頃の保存で
// 数式として取り込まれ #ERROR 化している。getFormula() は元の入力文字列
// (例: "=+819012345678") を返すため、先頭の "=" を外せば原文を安全に復元できる。
const recoverFormulaCell = (cell, row, label) => {
const formula = cell.getFormula(); // 数式でなければ空文字列
if (!formula) return false;
const restored = formula.replace(/^=/, '');
cell.setNumberFormat('@').setValue(restored);
formulaFixes.push('行' + row + ' ' + label + ': 数式化 ' + formula + ' → ' + restored);
return true;
};
for (let row = 2; row <= lastRow; row++) {
const pCell = sheet.getRange(row, phoneCol);
// 先に数式(=「+」残骸)を復元。復元したら数値分岐へは進まない。
if (!recoverFormulaCell(pCell, row, '電話番号')) {
const pVal = pCell.getValue();
if (typeof pVal === 'number') {
const restored = '0' + String(pVal);
pCell.setNumberFormat('@').setValue(restored);
phoneFixes.push('行' + row + ': ' + pVal + ' → ' + restored);
}
}
const aCell = sheet.getRange(row, acctCol);
if (!recoverFormulaCell(aCell, row, '口座番号')) {
const aVal = aCell.getValue();
if (typeof aVal === 'number') {
const asText = String(aVal);
aCell.setNumberFormat('@').setValue(asText);
acctReview.push('行' + row + ': ' + asText + '(先頭ゼロが必要か要確認)');
}
}
}
const report =
'✅ fixExistingContactData 完了\n\n' +
'「+」等の数式化セル復元: ' + formulaFixes.length + ' 件\n' + (formulaFixes.join('\n') || '(なし)') +
'\n\n電話番号 先頭ゼロ復元: ' + phoneFixes.length + ' 件\n' + (phoneFixes.join('\n') || '(なし)') +
'\n\n口座番号 文字列化: ' + acctReview.length + ' 件\n' + (acctReview.join('\n') || '(なし)');
Logger.log(report);
return report;
}
/**
* 【診断用・読み取り専用】各テンプレートの住所セル({{APPLICANT_ADDRESS}})の
* 合併状態・寸法・フォント・行高をログ出力する。エディタから手動実行。
* 住所が PDF で切れる原因(合併の向きなど)を特定するために使う。
*/
function inspectAddressCell() {
const ss = getMainSpreadsheet_();
const names = [SHEET_NAMES.TPL_OVERSEAS, SHEET_NAMES.TPL_WITH_WHT, SHEET_NAMES.TPL_WITHOUT_WHT];
const out = [];
names.forEach(name => {
const sh = ss.getSheetByName(name);
if (!sh) { out.push(name + ': シートなし'); return; }
const vals = sh.getDataRange().getValues();
let found = null;
for (let r = 0; r < vals.length && !found; r++) {
for (let c = 0; c < vals[r].length; c++) {
if (typeof vals[r][c] === 'string' && vals[r][c].indexOf('{{APPLICANT_ADDRESS}}') >= 0) {
found = { r: r + 1, c: c + 1 };
break;
}
}
}
if (!found) { out.push(name + ': {{APPLICANT_ADDRESS}} 見つからず'); return; }
const cell = sh.getRange(found.r, found.c);
let info = name + ': 住所=' + cell.getA1Notation() + ' merged=' + cell.isPartOfMerge();
if (cell.isPartOfMerge()) {
const m = cell.getMergedRanges()[0];
info += ' range=' + m.getA1Notation() + '(' + m.getNumRows() + '行x' + m.getNumColumns() + '列)';
}
info += ' colW=' + sh.getColumnWidth(found.c) + ' rowH=' + sh.getRowHeight(found.r) + ' font=' + cell.getFontSize();
out.push(info);
});
const report = out.join('\n');
Logger.log(report);
return report;
}
function validatePayload_(p) {
if (!p) throw new Error('payload missing');
const required = ['name', 'address', 'phone', 'email', 'bankName', 'branchName', 'accountName', 'accountNumber', 'residence', 'currency'];
required.forEach(k => { if (!p[k]) throw new Error('Missing field: ' + k); });
if (['japan', 'overseas'].indexOf(p.residence) < 0) throw new Error('Invalid residence');
if (!Array.isArray(p.items) || p.items.length === 0) throw new Error('No items');
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(p.email)) throw new Error('Invalid email format');
const items = p.items
.filter(it => it && it.name && Number(it.quantity) > 0 && Number(it.unitPrice) >= 0)
.map(it => ({
name: String(it.name).slice(0, 200),
quantity: Number(it.quantity),
unitPrice: Number(it.unitPrice)
}));
if (items.length === 0) throw new Error('No valid items');
return {
language: ['ja-JP', 'en-US', 'zh-TW', 'es-ES'].indexOf(p.language) >= 0 ? p.language : 'ja-JP',
residence: p.residence,
name: String(p.name).slice(0, 100),
address: String(p.address).slice(0, 300),
phone: String(p.phone).slice(0, 50),
email: String(p.email).slice(0, 200),
bankName: String(p.bankName).slice(0, 100),
branchName: String(p.branchName).slice(0, 100),
accountName: String(p.accountName).slice(0, 100),
accountNumber: String(p.accountNumber).slice(0, 50),
currency: ['JPY', 'TWD', 'USD', 'EUR'].indexOf(p.currency) >= 0 ? p.currency : 'JPY',
items: items,
notes: String(p.notes || '').slice(0, 500)
};
}
function notifyAdminOfPdfFailure_(payload, rowNumber, error) {
const settings = getSettings_();
const to = settings.notification_email;
if (!to) return;
const companyName = settings['company_name_ja-JP'] || 'Invoice System';
const subject = '[' + companyName + '] ⚠️ 請求書PDF自動生成失敗:' + payload.name;
const body = [
'PDFの自動生成に失敗しました。手動対応をお願いします。',
'',
'氏名:' + payload.name,
'居住地:' + (payload.residence === 'japan' ? '日本国内' : '日本国外'),
'通貨:' + payload.currency,
'',
'エラー内容:',
String(error.message || error),
'',
'スプレッドシートで該当行(行 ' + rowNumber + ')を確認し、',
'メニュー「請求書 → ➡️ 選んだ行のPDFを生成」を手動実行してください。',
'',
'スプレッドシート:' + getMainSpreadsheet_().getUrl()
].join('\n');
MailApp.sendEmail({ to: to, subject: subject, body: body });
}
/* ──────────────────── PDF 生成 ──────────────────── */
/**
* メニューから選択された行(最終行)に対してPDF生成
*/
function generateInvoiceForActiveRow() {
const sheet = SpreadsheetApp.getActiveSheet();
if (sheet.getName() !== SHEET_NAMES.SUBMISSIONS) {
SpreadsheetApp.getUi().alert('申請データシートで実行してください。');
return;
}
const row = sheet.getActiveCell().getRow();
if (row < 2) {
SpreadsheetApp.getUi().alert('データ行を選択してください。');
return;
}
try {
const result = generateInvoiceForRow(row);
SpreadsheetApp.getUi().alert('PDF生成完了:' + result.invoiceNo + '\n' + result.pdfUrl);
} catch (err) {
SpreadsheetApp.getUi().alert('エラー:' + err.message);
}
}
/**
* すべての pending → approved 済み行を一括処理
*/
function generateAllApprovedInvoices() {
const ss = getMainSpreadsheet_();
const sheet = ss.getSheetByName(SHEET_NAMES.SUBMISSIONS);
const lastRow = sheet.getLastRow();
if (lastRow < 2) return;
const data = sheet.getRange(2, 1, lastRow - 1, SUBMISSION_COLUMNS.length).getValues();
let processed = 0;
let errors = [];
data.forEach((row, idx) => {
const reviewStatus = row[SUBMISSION_COLUMNS.indexOf('審査ステータス')];
const invoiceNo = row[SUBMISSION_COLUMNS.indexOf('請求書番号')];
if (reviewStatus === 'approved' && !invoiceNo) {
try {
generateInvoiceForRow(idx + 2);
processed++;
} catch (err) {
errors.push('行 ' + (idx + 2) + ': ' + err.message);
}
}
});
SpreadsheetApp.getUi().alert(
processed + ' 件の請求書を生成しました。' + (errors.length ? '\nエラー:\n' + errors.join('\n') : '')
);
}
/**
* 指定行の請求書PDFを生成
* @param {number} rowNumber - 申請データシートの行番号
* @param {string} [overrideInvoiceNo] - 既存番号を再利用する場合に指定(再生成用)
* @param {{skipEmail?: boolean}} [options] - skipEmail=true で管理者通知メールを送らない(一括再生成用)
*/
function generateInvoiceForRow(rowNumber, overrideInvoiceNo, options) {
options = options || {};
const ss = getMainSpreadsheet_();
const sheet = ss.getSheetByName(SHEET_NAMES.SUBMISSIONS);
const row = sheet.getRange(rowNumber, 1, 1, SUBMISSION_COLUMNS.length).getValues()[0];
const getCol = name => row[SUBMISSION_COLUMNS.indexOf(name)];
const residence = getCol('居住地');
const reviewStatus = getCol('審査ステータス');
const existingInvoiceNo = getCol('請求書番号');
const withholding = getCol('源泉適用');
if (existingInvoiceNo && !overrideInvoiceNo) throw new Error('既にPDF生成済みです: ' + existingInvoiceNo);
if (reviewStatus === 'rejected') throw new Error('差戻済みの行はPDF生成できません');
if (residence === 'japan' && withholding !== 'yes' && withholding !== 'no') {
throw new Error('源泉適用を yes/no で指定してください');
}
const items = JSON.parse(getCol('請求明細(JSON)'));
const currency = getCol('通貨');
const subtotal = items.reduce((s, it) => s + it.quantity * it.unitPrice, 0);
const settings = getSettings_();
const taxRates = resolveTaxRates_(settings);
let consumptionTax = 0;
let withholdingTax = 0;
let templateName = SHEET_NAMES.TPL_OVERSEAS;
if (residence === 'japan') {
consumptionTax = Math.floor(subtotal * taxRates.consumptionRate);
if (withholding === 'yes') {
withholdingTax = calculateWithholdingTax_(subtotal, taxRates);
templateName = SHEET_NAMES.TPL_WITH_WHT;
} else {
templateName = SHEET_NAMES.TPL_WITHOUT_WHT;
}
}
const grandTotal = subtotal + consumptionTax - withholdingTax;
const invoiceNo = overrideInvoiceNo || generateInvoiceNumber_();
// 消費税ラベルを実際に適用した税率で表示する(国内は設定税率、海外は非課税 0%)。
// PDF 上の「消費税 N%」表記が設定・計算と必ず一致するようにする。
const consumptionRatePct = residence === 'japan' ? taxRates.consumptionRate * 100 : 0;
const consumptionRateLabel = formatTaxRatePercent_(consumptionRatePct) + '%';
const issuerLine1 = settings['company_name_ja-JP'] ? ('発行元:' + settings['company_name_ja-JP']) : '';
const issuerLine2 = settings.company_address ? ('〒 ' + settings.company_address) : '';
let issuerLine3 = '';
if (settings.qualified_invoice_number) {
issuerLine3 = '登録番号:' + settings.qualified_invoice_number;
} else if (String(settings.show_corporate_number || '').toLowerCase() === 'yes' && settings.corporate_number) {
issuerLine3 = '法人番号:' + settings.corporate_number;
}
const pdfBlob = renderInvoicePDF_({
templateName: templateName,
invoiceNo: invoiceNo,
// 作成日(=請求日)は申請の C 列「タイムスタンプ」を用いる。
// PDF を再生成しても日付が再生成日に動かないようにするため(会計要望)。
issueDate: formatDate_(resolveIssueDate_(getCol('タイムスタンプ'))),
clientName: settings['company_name_ja-JP'] || 'Company',
applicantName: getCol('氏名'),
applicantAddress: getCol('住所'),
applicantPhone: getCol('電話番号'),
bankName: getCol('振込銀行名'),
branchName: getCol('支店名'),
accountName: getCol('口座名義'),
accountNumber: getCol('口座番号'),
items: items,
currency: currency,
residence: residence,
subtotal: subtotal,
consumptionTax: consumptionTax,
consumptionRateLabel: consumptionRateLabel,
withholdingTax: withholdingTax,
grandTotal: grandTotal,
notes: getCol('備考'),
issuerLine1: issuerLine1,
issuerLine2: issuerLine2,
issuerLine3: issuerLine3
});
const file = saveToDrive_(pdfBlob, invoiceNo, getCol('氏名'));
if (!options.skipEmail) {
sendInvoiceEmail_(file, invoiceNo, getCol('氏名'), grandTotal, currency, settings);
}
sheet.getRange(rowNumber, SUBMISSION_COLUMNS.indexOf('請求書番号') + 1).setValue(invoiceNo);
sheet.getRange(rowNumber, SUBMISSION_COLUMNS.indexOf('PDF生成日') + 1).setValue(new Date());
sheet.getRange(rowNumber, SUBMISSION_COLUMNS.indexOf('PDF URL') + 1).setValue(file.getUrl());
return { invoiceNo: invoiceNo, pdfUrl: file.getUrl() };
}
/* ──────────────────── 請求書番号生成 ──────────────────── */
function generateInvoiceNumber_() {
const ss = getMainSpreadsheet_();
const sheet = ss.getSheetByName(SHEET_NAMES.COUNTER);
const now = new Date();
const ym = Utilities.formatDate(now, 'Asia/Tokyo', 'yyyyMM');
const data = sheet.getDataRange().getValues();
let rowIdx = -1;
for (let i = 1; i < data.length; i++) {
if (String(data[i][0]) === ym) { rowIdx = i; break; }
}
let seq;
if (rowIdx < 0) {
sheet.appendRow([ym, 1]);
seq = 1;
} else {
seq = Number(data[rowIdx][1]) + 1;
sheet.getRange(rowIdx + 1, 2).setValue(seq);
}
return ym + '-' + String(seq).padStart(3, '0');
}
/* ──────────────────── 源泉所得税の計算 ──────────────────── */
/**
* 設定シートの税率を数値として解決する。
* 値が未設定・非数値・0 以下の場合は、日本の法定デフォルトにフォールバックする
* (セルの消し忘れ等で誤った税額を出さないための安全策)。
* @param {Object} settings - getSettings_() の返り値
* @returns {{consumptionRate:number, withholdingRate:number, withholdingThreshold:number, withholdingRateOver:number}}
*/
function resolveTaxRates_(settings) {
settings = settings || {};
const num = (v, fallback) => {
const n = Number(v);
return (isFinite(n) && n > 0) ? n : fallback;
};
return {
consumptionRate: num(settings.consumption_tax_rate, 0.10),
withholdingRate: num(settings.withholding_tax_rate, 0.1021),
withholdingThreshold: num(settings.withholding_threshold, WITHHOLDING_THRESHOLD),
withholdingRateOver: num(settings.withholding_tax_rate_over, 0.2042)
};
}
/**
* 源泉所得税を計算する。
* @param {number} amount - 課税対象額(小計)
* @param {Object} [rates] - resolveTaxRates_() の返り値。未指定なら法定デフォルト。
*/
function calculateWithholdingTax_(amount, rates) {
rates = rates || resolveTaxRates_({});
if (amount <= rates.withholdingThreshold) {
return Math.floor(amount * rates.withholdingRate);
}
return Math.floor(
rates.withholdingThreshold * rates.withholdingRate +
(amount - rates.withholdingThreshold) * rates.withholdingRateOver
);
}
/**
* 税率(小数または百分率)を表示用のパーセント数値文字列にする。
* 浮動小数点の誤差を丸め、末尾の余分な 0 を落とす。
* 例: 10.000000000000002 → "10"、 8 → "8"、 10.5 → "10.5"
* @param {number} pct - 百分率の値(例: 10 は 10%)
*/
function formatTaxRatePercent_(pct) {
return String(Math.round((Number(pct) || 0) * 100) / 100);
}
/**
* 税額計算の回帰テスト。Apps Script エディタから手動実行する(関数を選んで「実行」)。
* 設定シートの税率が実際に計算へ反映されること、不正値が安全にフォールバックすることを検証する。
* 注: 末尾アンダースコアを付けない(付けると実行ドロップダウンに表示されないため)。
*/
function test_taxCalculation() {
const assert = (cond, msg) => { if (!cond) throw new Error('FAIL: ' + msg); };
// 1. デフォルト税率(設定が空でも法定値)
const def = resolveTaxRates_({});
assert(def.consumptionRate === 0.10, 'default consumption 10%');
assert(def.withholdingRate === 0.1021, 'default withholding 10.21%');
assert(def.withholdingThreshold === 1000000, 'default threshold 100万');
assert(def.withholdingRateOver === 0.2042, 'default over 20.42%');
// 2. 消費税:設定変更が計算に反映される(これがバグの本丸)
const r8 = resolveTaxRates_({ consumption_tax_rate: '0.08' });
assert(Math.floor(1000000 * r8.consumptionRate) === 80000, 'consumption follows setting: 8% of 100万 = 80000');
assert(Math.floor(1000000 * def.consumptionRate) === 100000, 'consumption default: 10% of 100万 = 100000');
// 3. 源泉:100万以下は単一税率(500000*0.1021=51050)
assert(calculateWithholdingTax_(500000, def) === 51050, 'withholding under threshold');
// 4. 源泉:100万超は二段階(1,000,000*0.1021 + 200,000*0.2042 = 142940)
assert(calculateWithholdingTax_(1200000, def) === 142940, 'withholding over threshold (two-tier)');
// 5. 源泉率・基準額の設定変更が反映される(500000*0.05=25000、基準額200万に引上げ)
const rw = resolveTaxRates_({ withholding_tax_rate: '0.05', withholding_threshold: '2000000' });
assert(calculateWithholdingTax_(500000, rw) === 25000, 'withholding follows setting');
// 6. 不正値・空欄はデフォルトへフォールバック(誤課税防止)
const bad = resolveTaxRates_({ consumption_tax_rate: '', withholding_tax_rate: 'abc', withholding_threshold: '0' });
assert(bad.consumptionRate === 0.10, 'empty → default');
assert(bad.withholdingRate === 0.1021, 'non-numeric → default');
assert(bad.withholdingThreshold === 1000000, 'zero → default');
// 7. パーセント表示の整形(浮動小数点誤差・末尾ゼロ除去)
assert(formatTaxRatePercent_(0.10 * 100) === '10', 'format 10% (fp-safe)');
assert(formatTaxRatePercent_(0.08 * 100) === '8', 'format 8% (fp-safe)');
assert(formatTaxRatePercent_(0) === '0', 'format 0%');
assert(formatTaxRatePercent_(10.5) === '10.5', 'format 10.5%');
// 8. 消費税ラベルが税率に追従する(PDF 表記=設定=計算 の一致)
assert(formatTaxRatePercent_(def.consumptionRate * 100) + '%' === '10%', 'label default 10%');
assert(formatTaxRatePercent_(r8.consumptionRate * 100) + '%' === '8%', 'label follows setting 8%');
// 9. 「消費税 N%」固定ラベル検出パターン(fillTemplateValues_ と同一)
const LABEL_RE = /^消費税\s*[0-9]+(?:\.[0-9]+)?\s*%$/;
assert(LABEL_RE.test('消費税 10%'), 'matches 消費税 10%');
assert(LABEL_RE.test('消費税 0%'), 'matches 消費税 0%');
assert(LABEL_RE.test('消費税 8%'), 'matches 消費税 8%');
assert(!LABEL_RE.test('消費税の説明は別途 10% と記載'), 'does not match free text containing 10%');
assert(!LABEL_RE.test('源泉所得税'), 'does not match withholding label');
Logger.log('✅ test_taxCalculation: 全アサーション通過');
return 'OK';
}
/**
* 作成日(請求日)解決ロジックの回帰テスト。Apps Script エディタから手動実行する。
* タイムスタンプ優先・不正値フォールバックを検証する(作成日が再生成日にずれない保証)。
*/
function test_resolveIssueDate() {
const assert = (cond, msg) => { if (!cond) throw new Error('FAIL: ' + msg); };
const fmt = d => Utilities.formatDate(d, 'Asia/Tokyo', 'yyyy-MM-dd');
// 1. Date オブジェクトはそのまま使う(申請の C 列はフォームから Date で保存される)
const ts = new Date('2026-05-31T09:00:00+09:00');
assert(fmt(resolveIssueDate_(ts)) === '2026-05-31', 'Date passthrough');
// 2. 文字列の日付も解釈する(手入力・コピー行対策)
assert(fmt(resolveIssueDate_('2026-05-31T09:00:00+09:00')) === '2026-05-31', 'string date parsed');
// 3. 空欄・不正値は現在日時にフォールバック(落ちずに発行できる)
assert(resolveIssueDate_('') instanceof Date, 'empty → now');
assert(resolveIssueDate_(null) instanceof Date, 'null → now');
assert(resolveIssueDate_('not-a-date') instanceof Date, 'garbage → now');
assert(!isNaN(resolveIssueDate_(undefined).getTime()), 'undefined → valid now');
Logger.log('✅ test_resolveIssueDate: 全アサーション通過');
return 'OK';
}
/* ──────────────────── PDFレンダリング ──────────────────── */
function renderInvoicePDF_(data) {
const ss = getMainSpreadsheet_();
const template = ss.getSheetByName(data.templateName);
if (!template) throw new Error('テンプレートが見つかりません: ' + data.templateName);
const tempSheet = template.copyTo(ss);
tempSheet.setName('_temp_' + Date.now());
try {
fillTemplateValues_(tempSheet, data);
SpreadsheetApp.flush();
const pdfBlob = exportSheetAsPDF_(ss.getId(), tempSheet.getSheetId(), data.invoiceNo);
return pdfBlob;
} finally {
ss.deleteSheet(tempSheet);
}
}
/**
* 折り返しテキストに必要な高さ(px)をざっくり見積もる。
* 利用可能幅(px)とフォントサイズから 1 行に入る文字数を推定し、行数 × 行高で算出する。
* CJK・全角はフォント幅、ASCII は約半角として概算。切れ防止のため余裕を多めに取る。
* @param {number} availWidthPx - セル(合併時は合計)の幅
* @param {number} fontPt - フォントサイズ(pt)
* @param {string} text - 表示テキスト
*/
function estimateWrappedLines_(availWidthPx, fontPt, text) {
const fontPx = (fontPt || 10) * 1.33; // pt → px 概算
const cjk = /[ᄀ-]/; // CJK・全角はフォント幅、その他は約半角
// セル内パディング+プロポーショナルフォントの実測差ぶん、可用幅は控えめに見積もる。
// (広く見積もると 1 行あたりの想定文字数が増え、結果的に行数を過小評価=末尾が切れる)
const usable = Math.max(availWidthPx - 12, 10);
// 明示的な改行(\n)で行を分け、各行ごとに自動折り返し行数を加算する。
// (\n を無視すると複数行テキスト=備考などが大幅に過小評価され、末尾が切れる)
let total = 0;
String(text).split('\n').forEach(segment => {
if (segment.length === 0) { total += 1; return; } // 空行も 1 行ぶん高さを取る
let textPx = 0;
for (let i = 0; i < segment.length; i++) {
textPx += cjk.test(segment[i]) ? fontPx : fontPx * 0.62;
}
total += Math.max(1, Math.ceil(textPx / usable));
});
return Math.max(1, total);
}
function estimateWrappedHeight_(availWidthPx, fontPt, text) {
const fontPx = (fontPt || 10) * 1.33;
const lines = estimateWrappedLines_(availWidthPx, fontPt, text);
const lineHeightPx = Math.ceil(fontPx * 1.7) + 5;
// 最終行が PDF で切れる事故を防ぐため、全体に余裕(+1 行ぶん+上下マージン)を持たせる。
// PDF エクスポートは行高に収まらない折り返しテキストを overflow させず切り落とすため。
return (lines + 1) * lineHeightPx + 12;
}
/**
* セルの合併ブロック情報を返す。合併していなければ単一セルとして扱う。
* @returns {{topRow:number, numRows:number, leftCol:number, numCols:number, width:number}}
*/
function mergedBlock_(sheet, row, col) {
const cell = sheet.getRange(row, col);
if (cell.isPartOfMerge()) {
const m = cell.getMergedRanges()[0];
let width = 0;
for (let i = 0; i < m.getNumColumns(); i++) width += sheet.getColumnWidth(m.getColumn() + i);
return { topRow: m.getRow(), numRows: m.getNumRows(), leftCol: m.getColumn(), numCols: m.getNumColumns(), width: width };
}
return { topRow: row, numRows: 1, leftCol: col, numCols: 1, width: sheet.getColumnWidth(col) };
}
/**
* ブロックの総高さが neededPx に満たなければ、不足ぶんを最終行に加える。
* 縦合併・横合併・非合併のいずれでも安全。高さは縮めない(増やすだけ)。
*/
function ensureBlockHeight_(sheet, block, neededPx) {
let current = 0;
for (let i = 0; i < block.numRows; i++) current += sheet.getRowHeight(block.topRow + i);
if (neededPx > current) {
const lastRow = block.topRow + block.numRows - 1;
sheet.setRowHeight(lastRow, sheet.getRowHeight(lastRow) + (neededPx - current));
}
}
/**
* テキストが 1 行で availWidthPx に収まる最大フォント(pt)を探す。
* defaultPt から 0.5pt 刻みで minPt まで縮める。
* @returns {{pt:number, fits:boolean}} fits=false は minPt でも収まらないことを示す
*/
function fitFontOneLine_(availWidthPx, text, defaultPt, minPt) {
const cjk = /[ᄀ-]/;
const widthAt = pt => {
const px = pt * 1.33;
let w = 0;
for (let i = 0; i < text.length; i++) w += cjk.test(text[i]) ? px : px * 0.6;
return w;
};
const usable = Math.max(availWidthPx - 8, 10);
let pt = defaultPt;
while (pt > minPt && widthAt(pt) > usable) pt -= 0.5;
if (pt < minPt) pt = minPt;
return { pt: pt, fits: widthAt(pt) <= usable };
}
function fillTemplateValues_(sheet, data) {
const replacements = {
'{{ISSUE_DATE}}': data.issueDate,
'{{INVOICE_NO}}': data.invoiceNo,
'{{CLIENT_NAME}}': data.clientName,
'{{APPLICANT_NAME}}': data.applicantName,
'{{APPLICANT_ADDRESS}}': data.applicantAddress,
'{{APPLICANT_PHONE}}': data.applicantPhone,
'{{BANK_NAME}}': data.bankName,
'{{BRANCH_NAME}}': data.branchName,
'{{ACCOUNT_NAME}}': data.accountName,
'{{ACCOUNT_NUMBER}}': data.accountNumber,
'{{TOTAL_AMOUNT}}': formatCurrency_(data.grandTotal, data.currency),
'{{SUBTOTAL}}': formatCurrency_(data.subtotal, data.currency),
'{{CONSUMPTION_TAX}}': formatCurrency_(data.consumptionTax, data.currency),
'{{WITHHOLDING_TAX}}': '-' + formatCurrency_(data.withholdingTax, data.currency),
'{{GRAND_TOTAL}}': formatCurrency_(data.grandTotal, data.currency),
'{{NOTES}}': data.notes || (data.residence === 'overseas' ? '海外居住者の為非課税' : ''),
'{{ISSUER_LINE_1}}': data.issuerLine1 || '',
'{{ISSUER_LINE_2}}': data.issuerLine2 || '',
'{{ISSUER_LINE_3}}': data.issuerLine3 || ''
};
// 固定レイアウト版: 申請者情報は折り返さず「1行・縮小表示(shrink-to-fit)」で固定枠に収める。
// これで孤字・左右ズレが出ず、どの請求書も同じ版面になる。住所は全幅セルなので長くても1行で収まりやすい。
const SHRINK_PLACEHOLDERS = [
'{{APPLICANT_ADDRESS}}', '{{APPLICANT_NAME}}', '{{APPLICANT_PHONE}}',
'{{BANK_NAME}}', '{{BRANCH_NAME}}', '{{ACCOUNT_NAME}}', '{{ACCOUNT_NUMBER}}'
];
// 備考のみ固定枠内で折り返し(複数行可)。パディングは枠の行高で確保する。
const WRAP_PLACEHOLDERS = ['{{NOTES}}'];
// 数字に見えるが「文字列」として表示すべきプレースホルダー。
// setValues で General 書式のセルに "0987..." を書くと再び数値化され先頭ゼロが落ちるため、
// 値を書き込む前にセルをプレーンテキスト書式(@)にしておく。
const TEXT_PLACEHOLDERS = ['{{APPLICANT_PHONE}}', '{{ACCOUNT_NUMBER}}'];
// 「消費税 10%」のような固定税率ラベルを、実際に適用した税率へ自動補正するためのパターン。
// セル全体がこの形のときだけ置換し、備考など他テキストには触れない(誤置換防止)。
// これにより、旧テンプレートでもマイグレーション不要で表記が設定・計算と必ず一致する。
const CONSUMPTION_LABEL_RE = /^消費税\s*[0-9]+(?:\.[0-9]+)?\s*%$/;
const range = sheet.getDataRange();
const values = range.getValues();
const wrapCells = []; // {row, col, text} 折り返し対象セル
const shrinkCells = []; // {row, col, text} 1 行に縮小表示するセル
for (let r = 0; r < values.length; r++) {
for (let c = 0; c < values[r].length; c++) {
const cell = values[r][c];
if (typeof cell !== 'string') continue;
if (cell.indexOf('{{') >= 0) {
const needsWrap = WRAP_PLACEHOLDERS.some(p => cell.indexOf(p) >= 0);
const needsShrink = SHRINK_PLACEHOLDERS.some(p => cell.indexOf(p) >= 0);
const needsText = TEXT_PLACEHOLDERS.some(p => cell.indexOf(p) >= 0);
let replaced = cell;
Object.keys(replacements).forEach(key => {
replaced = replaced.split(key).join(replacements[key]);
});
values[r][c] = replaced;
// 先頭ゼロを保持: 値を書き込む前にセルをテキスト書式にする(setValues の再数値化対策)
if (needsText) {
sheet.getRange(r + 1, c + 1).setNumberFormat('@');
}
if (needsWrap) {
sheet.getRange(r + 1, c + 1).setWrap(true);
wrapCells.push({ row: r + 1, col: c + 1, text: replaced });
}
if (needsShrink) {
shrinkCells.push({ row: r + 1, col: c + 1, text: replaced });
}
} else if (data.consumptionRateLabel && CONSUMPTION_LABEL_RE.test(cell.trim())) {
values[r][c] = '消費税 ' + data.consumptionRateLabel;
}
}
}
range.setValues(values);
// 折り返し後の実レイアウトを確定させてから行高を調整する。
SpreadsheetApp.flush();
// 情報欄: 固定枠なので折り返さず、1 行に収まるようフォントを縮小(下限 7pt)、縦中央で配置。
// 下限でも収まらない極端な長さのときだけ、最後の手段として折り返し可にする。
shrinkCells.forEach(sc => {
const anchor = sheet.getRange(sc.row, sc.col);
const block = mergedBlock_(sheet, sc.row, sc.col);
let fontPt = 10;
try { fontPt = anchor.getFontSize() || 10; } catch (e) {}
const fit = fitFontOneLine_(block.width, sc.text, fontPt, 7);
anchor.setFontSize(fit.pt).setVerticalAlignment('middle').setWrap(!fit.fits);
});
// 備考: 折り返し・上揃え。内容に合わせて枠の高さを確保し、長文でも切れないようにする
// (固定高だと長い備考の末尾が PDF で切り落とされるため)。
wrapCells.forEach(wc => {
const block = mergedBlock_(sheet, wc.row, wc.col);
let fontPt = 10;
try { fontPt = sheet.getRange(wc.row, wc.col).getFontSize() || 10; } catch (e) {}
sheet.getRange(wc.row, wc.col).setVerticalAlignment('top').setWrap(true);
ensureBlockHeight_(sheet, block, estimateWrappedHeight_(block.width, fontPt, wc.text));
});
const itemTable = findItemTablePosition_(sheet);
if (!itemTable) {
throw new Error('テンプレートに「品名/数量/単価/金額」ヘッダーが見つかりません');
}
const usedRows = Math.min(data.items.length, itemTable.totalItemRows);
if (data.items.length > itemTable.totalItemRows) {
Logger.log('⚠️ 明細が ' + data.items.length + ' 件あり、テンプレート上限 ' +
itemTable.totalItemRows + ' 行を超えています。' +
(data.items.length - itemTable.totalItemRows) + ' 件が PDF に表示されません。');
}
// 品名は B:C 合併セル。合併幅で折り返し行数を見積もり、長ければ複数行+行高を確保する。
// 単一行なら縦中央、複数行なら上揃え(数量・単価・金額も揃える)。
const nameBlock = mergedBlock_(sheet, itemTable.itemStartRow, itemTable.nameCol);
const nameWidth = nameBlock.width;
for (let i = 0; i < usedRows; i++) {
const it = data.items[i];
const row = itemTable.itemStartRow + i;
const multiline = estimateWrappedLines_(nameWidth, 10, String(it.name)) >= 2;
const valign = multiline ? 'top' : 'middle';
sheet.getRange(row, itemTable.nameCol).setValue(it.name).setWrap(true).setVerticalAlignment(valign);
sheet.getRange(row, itemTable.qtyCol).setValue(it.quantity).setVerticalAlignment(valign);
sheet.getRange(row, itemTable.priceCol).setValue(formatCurrency_(it.unitPrice, data.currency)).setVerticalAlignment(valign);
sheet.getRange(row, itemTable.amountCol).setValue(formatCurrency_(it.quantity * it.unitPrice, data.currency)).setVerticalAlignment(valign);
if (multiline) {
ensureBlockHeight_(
sheet,
{ topRow: row, numRows: 1, leftCol: itemTable.nameCol, numCols: nameBlock.numCols, width: nameWidth },
estimateWrappedHeight_(nameWidth, 10, String(it.name))
);
}
}
if (usedRows < itemTable.totalItemRows) {
sheet.hideRows(itemTable.itemStartRow + usedRows, itemTable.totalItemRows - usedRows);
}
}
/**
* テンプレート内の明細表ヘッダー(品名/数量/単価/金額)の位置を自動検出する
* 小計の位置から明細表の最大行数も決定する
*/
function findItemTablePosition_(sheet) {
const data = sheet.getDataRange().getValues();
let headerRow = -1;
let nameCol = -1, qtyCol = -1, priceCol = -1, amountCol = -1;
for (let r = 0; r < data.length; r++) {
const row = data[r];
let foundName = -1, foundQty = -1, foundPrice = -1, foundAmount = -1;
for (let c = 0; c < row.length; c++) {
const cell = String(row[c] || '').trim();
if (cell === '品名') foundName = c + 1;
else if (cell === '数量') foundQty = c + 1;
else if (cell === '単価') foundPrice = c + 1;
else if (cell === '金額') foundAmount = c + 1;
}
if (foundName > 0 && foundQty > 0 && foundPrice > 0 && foundAmount > 0) {
headerRow = r + 1;
nameCol = foundName;
qtyCol = foundQty;
priceCol = foundPrice;
amountCol = foundAmount;
break;
}
}
if (headerRow < 0) return null;
let subtotalRow = -1;
for (let r = headerRow; r < data.length; r++) {
const row = data[r];
for (let c = 0; c < row.length; c++) {
const cell = String(row[c] || '').trim();
if (cell === '小計') {
subtotalRow = r + 1;
break;
}
}
if (subtotalRow > 0) break;
}
const totalItemRows = subtotalRow > 0 ? (subtotalRow - headerRow - 1) : 27;
return {
headerRow,
itemStartRow: headerRow + 1,
totalItemRows,
nameCol, qtyCol, priceCol, amountCol
};
}
function exportSheetAsPDF_(spreadsheetId, sheetId, fileName) {
const url = 'https://docs.google.com/spreadsheets/d/' + spreadsheetId + '/export?' + [
'format=pdf',
'size=A4',
'portrait=true',
'fitw=true',
'sheetnames=false',
'printtitle=false',
'pagenumbers=false',
'gridlines=false',
'fzr=false',
'gid=' + sheetId,
'top_margin=0.50',
'bottom_margin=0.50',
'left_margin=0.50',
'right_margin=0.50'
].join('&');
const response = UrlFetchApp.fetch(url, {
headers: { Authorization: 'Bearer ' + ScriptApp.getOAuthToken() },
muteHttpExceptions: true
});
if (response.getResponseCode() !== 200) {
throw new Error('PDF export failed: ' + response.getContentText().slice(0, 200));
}
return response.getBlob().setName(fileName + '.pdf');
}
function saveToDrive_(blob, invoiceNo, applicantName) {
const fileName = invoiceNo + '_' + applicantName + '_請求書.pdf';
blob.setName(fileName);
return ensurePdfFolder_().createFile(blob);
}
function ensurePdfFolder_() {
const settings = getSettings_();
if (settings.pdf_folder_id) {
try {
return DriveApp.getFolderById(settings.pdf_folder_id);
} catch (e) {
Logger.log('既存の pdf_folder_id が無効です。再作成します。');
}
}
const folder = DriveApp.createFolder('請求書PDF');
saveSettingValue_('pdf_folder_id', folder.getId());
Logger.log('PDF保存フォルダを作成しました: ' + folder.getUrl());
return folder;
}
function saveSettingValue_(key, value) {
const ss = getMainSpreadsheet_();
const sheet = ss.getSheetByName(SHEET_NAMES.SETTINGS);
const data = sheet.getRange(2, 1, sheet.getLastRow() - 1, 2).getValues();
for (let i = 0; i < data.length; i++) {
if (data[i][0] === key) {
sheet.getRange(i + 2, 2).setValue(value);
return;
}
}
sheet.appendRow([key, value, '']);
}
function sendInvoiceEmail_(file, invoiceNo, applicantName, grandTotal, currency, settings) {
const to = settings.notification_email;
if (!to) {
Logger.log('notification_email 未設定のためメール送信をスキップ');
return;
}
const companyName = settings['company_name_ja-JP'] || 'Invoice System';