サービス
サービスを探す
プロ人材を探す
仕事を探す
ブログを探す

function createInvoicePdfs() {
const ss = SpreadsheetApp.getActive();
const list = ss.getSheetByName('一覧');
const tpl = ss.getSheetByName('請求書');
const folder = DriveApp.getFolderById('保存先フォルダのID');
const last = list.getLastRow();
if (last < 2) return;
const rows = list.getRange(2, 1, last - 1, 4).getValues();
rows.forEach((r, i) => {
if (!r[0] || r[3]) return; // 宛名が空、または作成済みなら飛ばす
tpl.getRange('B3').setValue(r[0]); // 宛名
tpl.getRange('B5').setValue(r[2]); // 件名
tpl.getRange('E10').setValue(r[1]); // 金額
SpreadsheetApp.flush();
// 請求書シートだけを一時ファイルにコピーしてPDFにする
const tmp = SpreadsheetApp.create('tmp_invoice');
tpl.copyTo(tmp);
tmp.deleteSheet(tmp.getSheets()[0]);
SpreadsheetApp.flush();
const file = DriveApp.getFileById(tmp.getId());
folder.createFile(file.getAs('application/pdf').setName('請求書_' + r[0] + '.pdf'));
file.setTrashed(true);
list.getRange(i + 2, 4).setValue('済');
});
}