如何自动为Google Form副本添加每周起始日期标题后缀
解决方案
修改后的完整脚本
function duplicateWeeklyForms() { const templateFormId = '1WsWb-VHK36Hm5YY5YaZaA14RdM2GhIujPwqmxiFDMk8'; const destFolder = DriveApp.getFolderById("1zWmCz9BaJwctdcCfutUsvAs1_qQhledk"); const targetYear = 2023; // 可修改为目标年份 // 获取当年所有周一的日期 const mondays = getMondaysInYear(targetYear); // 遍历每个周一,复制表单并命名 mondays.forEach(monday => { // 格式化日期为 MM/DD/YYYY 格式 const formattedDate = Utilities.formatDate(monday, Session.getScriptTimeZone(), "MM/dd/yyyy"); const formTitle = `Training Requests ${targetYear} - ${formattedDate}`; // 复制表单到指定文件夹 DriveApp.getFileById(templateFormId).makeCopy(formTitle, destFolder); }); } // 辅助函数:获取指定年份的所有周一日期 function getMondaysInYear(year) { const mondays = []; const startDate = new Date(year, 0, 1); // 当年1月1日 // 找到当年第一个周一 while (startDate.getDay() !== 1) { // 1代表周一 startDate.setDate(startDate.getDate() + 1); } const endDate = new Date(year, 11, 31); // 当年12月31日 let currentDate = new Date(startDate); // 循环添加每周一,直到超过当年年底 while (currentDate <= endDate) { mondays.push(new Date(currentDate)); // 复制日期对象避免引用问题 currentDate.setDate(currentDate.getDate() + 7); } return mondays; }
关键说明
- 自动生成周一列表:
getMondaysInYear函数会计算指定年份的所有周一日期,从当年第一个周一开始,每周递增7天,直到年底。 - 日期格式化:使用
Utilities.formatDate将日期转为MM/DD/YYYY格式,确保标题符合需求。若需指定时区(而非脚本默认时区),可将Session.getScriptTimeZone()替换为具体时区字符串,例如"America/New_York"或"GMT+8"。 - 批量复制表单:遍历所有周一日期,为每个日期生成带后缀的表单标题,并将模板表单复制到目标文件夹。
使用方法
- 打开你的Google表单对应的脚本编辑器(工具 > 脚本编辑器)。
- 替换原有的
duplicateForm函数为上述代码。 - 修改
targetYear为你需要的年份(如2024)。 - 运行
duplicateWeeklyForms函数,授权后即可自动生成全年每周的报名表单。
内容的提问来源于stack exchange,提问作者Ross P
相关产品推荐
相关产品推荐

