``` function onFormSubmit() { // Open the target Google Sheet file const spreadsheetId = '1WCLEKQYYYKVbFzz9ZwaKH3IEEk8JdbmZgecrQHwc4bY'; const spreadsheet = SpreadsheetApp.openById(spreadsheetId); const responseSheetName = spreadsheet.getSheets()[0].getName() // 回應表單名稱 const responseSheetValue = spreadsheet.getSheets()[0].getDataRange().getValues() // 回應表單值 const templateSheet = spreadsheet.getSheets()[1] // 文件模板 const quotationSheetName = spreadsheet.getSheets()[1].getName() //文件模板名稱 const quotationSheetValue = spreadsheet.getSheets()[1].getDataRange().getValues() // 文件模板值 // 取得所有變數index,減少手打數字失誤 const companyNameIndex = responseSheetValue[0].indexOf('單位名稱 Company title') const contactPersonIndex = responseSheetValue[0].indexOf('聯絡人姓名 Name') const timeStamp = responseSheetValue[0].indexOf('Timestamp') const emailIndex = responseSheetValue[0].indexOf('Email address'); const phoneIndex = responseSheetValue[0].indexOf('聯絡人電話 Phone number'); const dateIndex = responseSheetValue[0].indexOf('導覽預約日期 Preferred Date'); const durationIndex = responseSheetValue[0].indexOf('預計時間 Event Duration'); const pplIndex = responseSheetValue[0].indexOf('導覽人數 Number of Participant'); const planIndex = responseSheetValue[0].indexOf('預約方案 Tour Plan'); const budgetIndex = responseSheetValue[0].indexOf('預算規劃 Budget'); const specialIndex = responseSheetValue[0].indexOf('其他特殊需求 Special Needs'); const vatIndex = responseSheetValue[0].indexOf('單位統編 VAT number'); // 初始化所有迴圈數據 const responseLength = responseSheetValue.length - 1 let countResponse = 1 const folderId = '1LXXa61BX3enR7d3oIk0BDQ4wb8Wp94BQ' // 跑動迴圈,產生新報表 for(countResponse; countResponse<=responseLength; countResponse++){ const newQuotationSheet = templateSheet.copyTo(spreadsheet); // 用模板開新文件 // 命名新建的表單 if (quotationSheetName) { try{ if(responseSheetValue[1][companyNameIndex]) newQuotationSheet.setName('Quote - ' + responseSheetValue[countResponse][companyNameIndex]) else if(responseSheetValue[1][contactPersonIndex]) newQuotationSheet.setName('Quote - ' + responseSheetValue[countResponse][contactPersonIndex]) else console.log('未填寫人名及公司名稱') } catch(e) { console.log('未知的錯誤狀況',e) } } // 建立表單 newQuotationSheet.getRange('D3').setValue(responseSheetValue[countResponse][timeStamp]) newQuotationSheet.getRange('B4').setValue(responseSheetValue[countResponse][companyNameIndex]); newQuotationSheet.getRange('D4').setValue(responseSheetValue[countResponse][vatIndex]) newQuotationSheet.getRange('B5').setValue(responseSheetValue[countResponse][contactPersonIndex]) newQuotationSheet.getRange('D5').setValue(responseSheetValue[countResponse][phoneIndex]) newQuotationSheet.getRange('B6').setValue(responseSheetValue[countResponse][emailIndex]) newQuotationSheet.getRange('A8').setValue(responseSheetValue[countResponse][dateIndex]) newQuotationSheet.getRange('B8').setValue(responseSheetValue[countResponse][durationIndex]) newQuotationSheet.getRange('C8').setValue(responseSheetValue[countResponse][pplIndex]) newQuotationSheet.getRange('D8').setValue(responseSheetValue[countResponse][planIndex]) newQuotationSheet.getRange('B8').setValue(responseSheetValue[countResponse][dateIndex]) newQuotationSheet.getRange('B9:C9').setValue(responseSheetValue[countResponse][specialIndex]) newQuotationSheet.getRange('D9').setValue(responseSheetValue[countResponse][budgetIndex]) } } ````