Google Apps Script:如何按文件ID获取文件并设置所需的文件类型等等。

huangapple go评论73阅读模式
英文:

Google Apps Script: How to getFileByID and set fileType as desired & more

问题

以下是您要翻译的内容:

以下是我正在编写的脚本中的一些片段,该脚本将通过Google表单提交电子表格以通过订单上传电子邮件发送。我需要确保我发送的文档(fileSheet)格式正确,以便我们的团队可以通过我们的OMS执行订单上传。订单上传可以通过“文本(制表符分隔)”文件类型来实现,主要是通过审查excel中提交的数据然后导出为所述文本文件。我会注意到,我看到有“text/tab-separated-values”的东西 - 除了excel表格之外,将这个也发送出去以减轻订单助手的繁琐工作不会有什么坏处。

不幸的是,我发现虽然通过表单上传的数据表以Datasheet的形式提交,但在从上传驱动器中通过ID提取后,它们以PDF的形式通过电子邮件发送(如下所示:DriveApp.getFileById(orderUploadResponse);)。我希望通过提取Blob,设置必要的参数,然后将此文件推送给协调人来解决这个问题。最好的是新创建的文件仅用于通过电子邮件发送,而不是通过Drive保存。

(注:下面的写作是我最接近的尝试)

// 获取数据表的关键字
var orderUploadResponse = items[1].getResponse();

// 指定时间ID
var timeFormatted = Utilities.formatDate(time, "CDT", "'MNL'-MMddyyyy-hhmm");

// 从Google Drive中获取通知附件文件
try {
var fileSheetByID = DriveApp.getFileById(orderUploadResponse);
var fileSheet = fileSheetByID.getBlob();
fileSheet.setContentType('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet').setName(timeFormatted);
DriveApp.createFile(fileSheet);
}
catch (errXL) {
var fileSheet = DriveApp.getFileById(orderUploadResponse);
Logger.log("数据表(Blob 失败):" + errXL);
}
英文:

Listed below are some snippets from a script I'm writing that will send a spreadsheet over an order upload email via Google Form submission. I need to ensure the document I send (fileSheet) is properly formatted so that our team can perform an order upload through our OMS. Order uploads can be achieve via "Text (Tab Delimited)" fileType, which is done primarily by reviewing submitted data in excel then exporting as stated text file. I will note, I saw there is such thing as "text/tab-separated-values" - wouldn't hurt to send this in addition to the excel sheet to cut down on the tediousness put onto the order facilitators.

Unfortunately, I've found that although the datasheets uploaded thru the form are submitted as Datasheets, they are emailed as PDF's after being pulled from the upload drive by ID (Seen below via DriveApp.getFileById(orderUploadResponse);). I'm looking to work around this by pulling the Blob, setting the necessary parameters, then pushing this file to the facilitators. It would be favorable that the new file made is only created to be sent over email and not saved via Drive.

(Note: The write-up below is my closest attempt)

// Get key for datasheet
var orderUploadResponse = items[1].getResponse();

// Specify time ID
var timeFormatted = Utilities.formatDate(time, "CDT", "'MNL'-MMddyyyy-hhmm");

// Grab files from Google Drive for notification attachments
try {
var fileSheetByID = DriveApp.getFileById(orderUploadResponse);
var fileSheet = fileSheetByID.getBlob();
fileSheet.setContentType('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet').setName(timeFormatted);
DriveApp.createFile(fileSheet);
}
catch (errXL) {
var fileSheet = DriveApp.getFileById(orderUploadResponse);
Logger.log("Datasheet (Blob Failed): " + errXL);
}

答案1

得分: 0

The purpose of this script is to send an email on submission of a form, and to attach an uploaded Excel file to the email.

The script should be installed as an onFormSubmit trigger.

The recipient email must be inserted at line 28.

英文:

The purpose of this script is to send an email on submission of a form, and to attach an uploaded Excel file to the email.

The script should be installed as an onFormSubmit trigger.

The recipient email must be inserted at line 28.


/*
// this function to be an installable "onFormSubmit" trigger.
// the function sends an email 
// edit the recipient email address
*/
function sendEmail(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet = ss.getSheetByName("Form responses 1")

var editedRow = e.range.rowStart
var lc = sheet.getLastColumn()
var dataRange = sheet.getRange(editedRow,1,1,lc)
// Logger.log("DEBUG: data range = "+dataRange.getA1Notation())
var data = dataRange.getValues()
var uploadURL = data[0][2]
var indexOfid = uploadURL.indexOf("id=");
var uploadID = uploadURL.substring(+indexOfid+3)
// Logger.log("DEBUG: url = "+uploadURL)
// Logger.log("DEBUG: id = "+uploadID)

// Specify time ID
var timeFormatted = Utilities.formatDate(new(Date), "CDT", "'MNL'-MMddyyyy-hhmm");


// Send an email with a file from Google Drive attached as a Excel.
var xlsxMT = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
var file = DriveApp.getFileById(uploadID);
GmailApp.sendEmail('<<insert recipient email>>', 'Attachment example', 'Please see the attached file.', {
      attachments: [file.getAs(xlsxMT)] // modified
    });
}

huangapple
  • 本文由 发表于 2023年4月17日 23:13:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76036655.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定