我没有任何编码背景,因为我来自医学方面。我已经编写了代码来将我的 Google 表格数据更新到各自的患者个人文档文件中。我已将文档文件的所有 ID 收集到一列中,并通过循环使用
getfilesbyid
来更新每个患者的详细信息。但是文件将列数据作为 i/p 参数传递会导致运行脚本时发生错误。
这是我写的代码:
onOpen() {
const ui = SpreadsheetApp.getUi();
const menu = ui.createMenu('Create Assessment Form');
menu.addItem('Create New Docs', 'createNewGoogleDocs');
menu.addToUi();
}
function createNewGoogleDocs() {
const rows = sheet.getDataRange().getValues();
rows.forEach(function(row, index) {
if (index === 0 || row[15])
return;
const Patientid = row[0];
const Name = row[1];
const Day1 = row[2];
const Day2 = row[3];
const Day3 = row[4];
const Day4 = row[5];
const Day5 = row[6];
const Day6 = row[7];
const Day7 = row[8];
const Day8 = row[9];
const Day9 = row[10];
const Day10 = row[11];
const templateid = row[12];
const googleDocTemplate = DriveApp.getFileById('temlateid');
const doc = DocumentApp.openById(googleDocTemplate.getId());
const body = doc.getBody();
body.replaceText('{{Name}}', Name);
body.replaceText('{{PatientID}}', Patientid);
body.replaceText('{{Day1}}', Day1);
body.replaceText('{{Day2}}', Day2);
body.replaceText('{{Day3}}', Day3);
body.replaceText('{{Day4}}', Day4);
body.replaceText('{{Day5}}', Day5);
body.replaceText('{{Day6}}', Day6);
body.replaceText('{{Day7}}', Day7);
body.replaceText('{{Day8}}', Day8);
body.replaceText('{{Day9}}', Day9);
body.replaceText('{{Day10}}', Day10);
doc.saveAndClose();
});
}
如果有任何错误,请纠正我,并为由于我的知识缺乏而造成的错误提前道歉。
我修改了你的脚本,它将根据你输入的数据生成一个新的 Google 文档,它也会自动保存在你的 Google 云端硬盘上。
请尝试使用下面的脚本。
function onOpen() {
const ui = SpreadsheetApp.getUi();
const menu = ui.createMenu('Create Assessment Form');
menu.addItem('Create New Docs', 'createNewGoogleDocs')
menu.addToUi();
}
function createNewGoogleDocs() {
const googleDocTemplate = DriveApp.getFileById('Your_Doc_ID'); //Change with your actual Template Doc ID
const destinationFolder = DriveApp.getFolderById('Your_Folder_ID'); //Change with your actual Google Drive Folder ID
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1'); //Replace with your Sheet Name
const rows = sheet.getDataRange().getValues();
rows.forEach(function (row, index) {
if (index === 0 || row[14]) return;
const Patientid = row[0];
const Name = row[1];
const Day1 = row[2];
const Day2 = row[3];
const Day3 = row[4];
const Day4 = row[5];
const Day5 = row[6];
const Day6 = row[7];
const Day7 = row[8];
const Day8 = row[9];
const Day9 = row[10];
const Day10 = row[11];
const templateid = row[12];
const copy = googleDocTemplate.makeCopy(`Patient: ${row[1]}`, destinationFolder) //Document Name
const doc = DocumentApp.openById(copy.getId())
const body = doc.getBody();
body.replaceText('{{Name}}', Name);
body.replaceText('{{PatientID}}', Patientid);
body.replaceText('{{Day1}}', Day1);
body.replaceText('{{Day2}}', Day2);
body.replaceText('{{Day3}}', Day3);
body.replaceText('{{Day4}}', Day4);
body.replaceText('{{Day5}}', Day5);
body.replaceText('{{Day6}}', Day6);
body.replaceText('{{Day7}}', Day7);
body.replaceText('{{Day8}}', Day8);
body.replaceText('{{Day9}}', Day9);
body.replaceText('{{Day10}}', Day10);
doc.saveAndClose();
const url = doc.getUrl();
sheet.getRange(index + 1, 15).setValue(url) //The row where the Document link will be stored
})
}
注意:在线
和8
时,请务必将其替换为您实际的9
和Document ID
Google Drive Folder ID
一些注意事项:
patient's name
。您也可以根据自己的喜好进行更改。