我有一个连接到谷歌表单的电子表格,我希望我们的员工能够填写并填充一个模板,将完成的模板转换为PDF文件,然后将PDF文件发送到表单中收集的电子邮件地址。直到最近,当我开始收到“例外:访问被拒绝:DriveApp”的错误消息时,表单才开始工作。我对应用程序脚本编程是新手,所以我很难追踪错误的原因,但我认为这可能是因为我将表单和相关文件移动到了我不是所有者的驱动器上。然而,我昨天把所有东西都移回了我的个人硬盘,仍然收到同样的错误。理想情况下,我希望表单能在我不是所有者的情况下使用,但我自己和其他员工都可以使用,但是我不确定如何实现这一点。
这是我的代码。我大部分时间都使用视频教程,因为我刚开始编码。错误出现在createPDF(代码:22:35)和测试(代码:13:3)。
//function createTrigger() {
//let ss = SpreadsheetApp.getActiveSpreadsheet();
//ScriptApp.newTrigger('test')
//.forSpreadsheet(ss)
//.onFormSubmit()
//.create();
//creates trigger from within spreadsheet
//}
function test(e){
const info = e.namedValues;
Logger.log (info); //Log the info for review
createPDF(info);
}
function createPDF(info){
//creates PDF from template, emails to case file and staff, and logs filename and file URL in spreadsheet
const pdfFolder=DriveApp.getFolderById("(folderID)");
const tempFolder = DriveApp.getFolderById("(folderID)");
const templateDoc= DriveApp.getFileById("(FileID)");
const newTempFile = templateDoc.makeCopy(tempFolder);
const openDoc = DocumentApp.openById(newTempFile.getId());
const body = openDoc.getBody();
var caseEmail = info\['Case Number'\]\[0\]+"@xxx.org";
var fileName = "HT Long Form Screening Doc: " + info\['Case Number'\]\[0\] + "-" +info\['Timestamp'\]\[0\]+ "-"+info\['Staff Name'\]\[0\];//this works
var tempFile = DriveApp.getFileById(newTempFile);
Logger.log(tempFile);
console.log(createPDF);
body.replaceText("{ts}", info\['Timestamp'\]\[0\]);
body.replaceText("{staffemail}", info\['Email'\]\[0\]);
body.replaceText("{staffname}",info\['Staff Name'\]\[0\]);
body.replaceText("{staffproject}", info\['Project'\]\[0\]);
body.replaceText("{casenumber}", info\['Case Number'\]\[0\]);
body.replaceText("{calldate}", info\['Call Date'\]\[0\]);//ok
body.replaceText("{calltime}", info\['Call Time'\]\[0\]);//ok
body.replaceText("{immediateaction}", info\['Immediate Action'\]\[0\]);//ok
body.replaceText("{immediateactiondetail}", info\['Immediate Action Detail'\]\[0\]);//ok
body.replaceText("{casemail}", caseEmail);//ok
body.replaceText("{referralsource}",info\['Referral Source'\]\[0\]);
body.replaceText("{referralsourcedetail}",info\['Referral Source Detail'\]\[0\]);
body.replaceText("{officialdetermination}",info\['Official Determination'\]\[0\]);
body.replaceText("{officialdeterminationdetail}",info\['Official Determination Detail'\]\[0\]);
body.replaceText("{personalsafety}",info\['Personal Safety'\]\[0\]);
body.replaceText("{safehousing}",info\['Safe and Stable Housing'\]\[0\]);
body.replaceText("{fairhousing}",info\['Fair Housing Referral'\]\[0\]);
body.replaceText("{survivorname}",info\['Survivor Name'\]\[0\]);
body.replaceText("{survivorphone}",info\['Survivor phone number'\]\[0\]);
body.replaceText("{survivoremail}",info\['Survivor email address'\]\[0\]);
body.replaceText("{survivoraddress}",info\['Survivor address'\]\[0\]);
body.replaceText("{survivorcounty}",info\['Survivor County of Residence'\]\[0\]);
body.replaceText("{callertype}",info\['Type of Caller'\]\[0\]);
body.replaceText("{gender}",info\['Gender'\]\[0\]);
body.replaceText("{interviewlanguage}",info\['Interview Language'\]\[0\]);
body.replaceText("{preferredlanguage}",info\['Preferred Language'\]\[0\]);
body.replaceText("{interpreter}",info\['Interpreter Needed'\]\[0\]);
body.replaceText("{callername}",info\['Caller Name'\]\[0\]);
body.replaceText("{providername}",info\['Provider Organization'\]\[0\]);
body.replaceText("{familyfriendname}",info\['Family_Friend Name'\]\[0\]);
body.replaceText("{familyfriendphone}",info\['Family_Friend Phone'\]\[0\]);
body.replaceText("{familyfriendrelationship}",info\['Family_Friend Relationship'\]\[0\]);
body.replaceText("{survivordob}",info\['Survivor DOB'\]\[0\]);
body.replaceText("{yearsofschooling}",info\['Years of Schooling'\]\[0\]);
body.replaceText("{citizenshipcountries}",info\['Citizenship Countries'\]\[0\]);
body.replaceText("{birthcountry}",info\['Birth Country'\]\[0\]);
body.replaceText("{whyleftcountry}",info\['Why Left Country'\]\[0\]);
body.replaceText("{threemonthscountry}",info\['3 Months Country'\]\[0\]);
body.replaceText("{recentarrival}",info\['Recent Arrival'\]\[0\]);
body.replaceText("{arrangetravel}",info\['Arrange Travel?'\]\[0\]);
body.replaceText("{whoarrangedtravel}",info\['Who Arranged Travel'\]\[0\]);
body.replaceText("{arrangetraveldetail}",info\['Arranged Travel Detail'\]\[0\]);
body.replaceText("{pressuredyou}",info\['Pressured You'\]\[0\]);
body.replaceText("{pressuredyoudetail}",info\['Pressured You Detail'\]\[0\]);
body.replaceText("{totalcost}",info\['Total Cost'\]\[0\]);
body.replaceText("{borrowmoney}",info\['Borrow Money'\]\[0\]);
body.replaceText("{existingdebt}",info\['Existing Debt'\]\[0\]);
body.replaceText("{existingdebtdetail}",info\['Existing Debt Detail'\]\[0\]);
body.replaceText("{pressuredtopay}",info\['Pressured to Pay'\]\[0\]);
body.replaceText("{pressuredtopaydetail1}",info\['Pressured to Pay Detail 1'\]\[0\]);
body.replaceText("{pressuredtopaydetail2}",info\['Pressured to Pay Detail 2'\]\[0\]);
body.replaceText("{supposedtobepaidforwork}",info\['Supposed to be paid for work'\]\[0\]);
body.replaceText("{kindofwork}",info\['Kind of Work'\]\[0\]);
body.replaceText("{foundoutaboutwork}",info\['How found out about work'\]\[0\]);
body.replaceText("{didnotgetpaid}",info\['Not Paid'\]\[0\]);
body.replaceText("{kindofwork2}",info\['Kind of work 2'\]\[0\]);
body.replaceText("{kindofwork3}",info\['Kind of work 3'\]\[0\]);
body.replaceText("{paymentexpected}",info\['Payment expected'\]\[0\]);
body.replaceText("{paymentreceived}",info\['Payment received'\]\[0\]);
body.replaceText("{hownotpaid}",info\['How not paid'\]\[0\]);
body.replaceText("{differentwork}",info\['Different work than expected'\]\[0\]);
body.replaceText("{workpromised}",info\['What were you promised'\]\[0\]);
body.replaceText("{actualwork}",info\['What actual work'\]\[0\]);
body.replaceText("{workfeelscared}",info\['Work feel scared'\]\[0\]);
body.replaceText("{workfeelscareddetail}",info\['Work feel scared detail'\]\[0\]);
body.replaceText("{workhurt}",info\['Work hurt'\]\[0\]);
body.replaceText("{workhurtdetail}",info\['Work hurt detail'\]\[0\]);
body.replaceText("{workthreats}",info\['Work threats'\]\[0\]);
body.replaceText("{workthreatsdetail}",info\['Work threats detail'\]\[0\]);
body.replaceText("{tworktakebreaks}",info\['Work take breaks'\]\[0\]);
body.replaceText("{worksickoremergency}",info\['Work sick or emergency'\]\[0\]);
body.replaceText("{worktakebreaksdetail}",info\['Work take breaks detail'\]\[0\]);
body.replaceText("{askpermission}",info\['Have to ask permission'\]\[0\]);
body.replaceText("{nopermission}",info\['What if no permission'\]\[0\]);
body.replaceText("{workinjured}",info\['Work injured'\]\[0\]);
body.replaceText("{workinjurednomedical}",info\['Work injured stopped from medical'\]\[0\]);
body.replaceText("{workinjurednomedicaldetail}",info\['Work injured stopped from medical detail'\]\[0\]);
body.replaceText("{workpressured}",info\['Work pressured'\]\[0\]);
body.replaceText("{workpressureddetail}",info\['Work pressured detail'\]\[0\]);
body.replaceText("{workphotos}",info\['Work photos'\]\[0\]);
body.replaceText("{workphotoswho}",info\['Work photos who'\]\[0\]);
body.replaceText("{workphotoswhatplanned}",info\['Work photos plan to do'\]\[0\]);
body.replaceText("{workphotosagreement}",info\['Work photos agreement'\]\[0\]);
body.replaceText("{worksexforvalue}",info\['Work sex for value'\]\[0\]);
body.replaceText("{worksexforvaluepressured}",info\['Work sex for value pressured'\]\[0\]);
body.replaceText("{worsexforvalueage}",info\['Work sex for value age'\]\[0\]);
body.replaceText("{worktakeid}",info\['Work take ID'\]\[0\]);
body.replaceText("{worktakeidgetback}",info\['Work take ID could retrieve'\]\[0\]);
body.replaceText("{worktakeidwhathappened}",info\['Work take ID what happened'\]\[0\]);
body.replaceText("{worktakemoney}",info\['Work take money'\]\[0\]);
body.replaceText("{worktakemoneyagreed}",info\['Work take money agreed'\]\[0\]);
body.replaceText("{worktakemoneydetail}",info\['Work take money detail'\]\[0\]);
body.replaceText("{whereliving}",info\['Where living'\]\[0\]);
body.replaceText("{livesameplacework}",info\['Live same place work'\]\[0\]);
body.replaceText("{livesameplaceworkdetail}",info\['Live same place work detail'\]\[0\]);
body.replaceText("{workcouldnotleave}",info\['Work felt could not leave'\]\[0\]);
body.replaceText("{workcouldnotleavedetail}",info\['Work felt could not leave detail'\]\[0\]);
body.replaceText("{worktriedtoleave}",info\['Work what if tried to leave'\]\[0\]);
body.replaceText("{worklockedin}",info\['Work locked in'\]\[0\]);
body.replaceText("{worklockedindetail}",info\['Work locked in detail'\]\[0\]);
body.replaceText("{workmonitored}",info\['Work monitored'\]\[0\]);
body.replaceText("{workmonitoreddetail}",info\['Work monitored detail'\]\[0\]);
body.replaceText("{workoutsidecontact}",info\['Work outside contact'\]\[0\]);
body.replaceText("{workfalseid}",info\['Work false ID'\]\[0\]);
body.replaceText("{workfalseiddetail}",info\['Work false ID detail'\]\[0\]);
body.replaceText("{workreporttocops}",info\['Work report to cops'\]\[0\]);
body.replaceText("{workreporttocopsdetail}",info\['Work report to cops detail'\]\[0\]);
body.replaceText("{workothersthreatened}",info\['Work others threatened'\]\[0\]);
body.replaceText("{workothersthreateneddetail}",info\['Work others threatened detail'\]\[0\]);
body.replaceText("{workfoodcontrol}",info\['Work food control'\]\[0\]);
body.replaceText("{workfoodcontroldetail}",info\['Work food control detail'\]\[0\]);
body.replaceText("{worksleepcontrol}",info\['Work sleep control'\]\[0\]);
body.replaceText("{worksleepcontroldetail}",info\['Work sleep control detail'\]\[0\]);
body.replaceText("{worklanguage}",info\['Work language difficulties'\]\[0\]);
body.replaceText("{indicators}",info\['Victimization indicators'\]\[0\]);
body.replaceText("{inaccurate}",info\['Inaccurate responses'\]\[0\]);
body.replaceText("{likelihood}",info\['Likelihood'\]\[0\]);
body.replaceText("{ratingreason}",info\['Rating reason'\]\[0\]);
body.replaceText("{referralstomake}",info\['Referrals to be made'\]\[0\]);
body.replaceText("{notes}",info\['Additional notes'\]\[0\]);
openDoc.saveAndClose();//works but should it delete the temp file now?
const blobPDF=newTempFile.getAs(MimeType.PDF);
const pdfFile = pdfFolder.createFile(blobPDF).setName(fileName);
//tempFolder.removeFile(tempFile);//not sure that this works
var newFileID = pdfFile.getId();
var subject = 'Screening Note to File';
var emailBody = info\['Case Number'\]\[0\] + " " + info\['Staff Name'\]\[0\];
var caseEmail = info\['Case Number'\]\[0\]+"@xxx.org";//this works
var fileName = info\['Case Number'\]\[0\] + " " +info\['Timestamp'\]\[0\];
var staffemail = info\['Email'\]\[0\];
Logger.log(caseEmail);
Logger.log(fileName);
DriveApp.getFileById(newFileID);
GmailApp.sendEmail(caseEmail, subject, emailBody,{attachments:pdfFile});
GmailApp.sendEmail(staffemail, subject, emailBody, {attachments:pdfFile});
var htFileURL = pdfFile.getUrl();
Logger.log(htFileURL);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var sheet1 = sheets\[0\];
var cell = sheet1.getActiveCell();
var selectRow = cell.getRow();
Logger.log ("SelectRow: " + selectRow);
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form Responses 1").getRange(selectRow,120).setValue(htFileURL);
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form Responses 1").getRange(selectRow, 121).setValue(fileName);
}