تحويل جداول بيانات Google والبريد الإلكتروني كملفات PDF

يمكنك إعداد وظيفة CRON في Google Drive باستخدام برنامج Google Apps Script الذي سيرسل أي جدول بيانات Google ، أو أي مستند أو ملف آخر في محرك الأقراص ، إلى عناوين البريد الإلكتروني أو أكثر في وقت محدد. يمكنك ضبط الزناد المستند إلى الوقت لتشغيل أسبوعيًا ، يوميًا ، كل ساعة وغيرها من الجداول المتكررة.
يوضح هذا المثال كيفية إرسال جدول بيانات Google إلى عنوان البريد الإلكتروني المحدد تلقائيًا كملف PDF يوميًا. يقوم برنامج Google Script بتحويل جدول بيانات Google إلى ملف PDF ويرسله إلى عنوان بريد إلكتروني آخر باستخدام حساب Gmail الخاص بك. يمكنك مزيد من تخصيص إخراج PDF – مثل إزالة الشبكات ، وإخفاء الصفوف المجمدة ، والتغيير إلى وضع المناظر الطبيعية ، وما إلى ذلك عن طريق تعيين معلمات التصدير الصحيحة.
تحويل وإرسال أوراق Google عبر البريد الإلكتروني
ال البريد الإلكتروني جدول بيانات Google يمكن الإضافية تلقائيًا تحويل جداول البيانات والبريد الإلكتروني في تنسيقات PDF أو CSV أو Microsoft Excel (XLSX). يمكن أن يحول جدول البيانات أو الأوراق الفردية بأكملها.
يمكن للإصدار الممتاز للإضافة تلقائيًا إرسال الرسوم المحولة تلقائيًا على أ جدول متكرر (مثل كل ساعة ، يوميًا أو أسبوعيًا أو شهريًا). يمكنك أيضًا إعداد جداول إرسال متعددة وإرسال الورقة تلقائيًا إلى مجموعة مختلفة من الإيصالات في أوقات مختلفة.
نص Google للبريد الإلكتروني من جداول بيانات Google
إذا لم تتمكن من استخدام الوظيفة الإضافية لقرار البريد الإلكتروني (بعض مدراء تطبيقات Google لا يسمحون بالمواد الإضافية) ، فيمكنك كتابة نص Google الخاص بك لإرسال بريد إلكتروني إلى جدول البيانات كملفات PDF.
/* Send Spreadsheet in an email as PDF, automatically */
function emailSpreadsheetAsPDF() {
// Send the PDF of the spreadsheet to this email address
const email = Session.getActiveUser().getEmail() || "amit@labnol.org";
// Get the currently active spreadsheet URL (link)
// Or use SpreadsheetApp.openByUrl("<>");
const ss = SpreadsheetApp.getActiveSpreadsheet();
// Subject of email message
const subject = `PDF generated from spreadsheet ${ss.getName()}`;
// Email Body can be HTML too with your logo image - see ctrlq.org/html-mail
const body = "Sent with [Email Google Sheets](/email-sheet)";
// Base URL
const url = "https://docs.google.com/spreadsheets/d/SS_ID/export?".replace("SS_ID", ss.getId());
const exportOptions =
"exportFormat=pdf&format=pdf" + // export as pdf / csv / xls / xlsx
"&size=letter" + // paper size legal / letter / A4
"&portrait=false" + // orientation, false for landscape
"&fitw=true&source=labnol" + // fit to page width, false for actual size
"&sheetnames=false&printtitle=false" + // hide optional headers and footers
"&pagenumbers=false&gridlines=false" + // hide page numbers and gridlines
"&fzr=false" + // do not repeat row headers (frozen rows) on each page
"&gid="; // the sheet's Id
const token = ScriptApp.getOAuthToken();
const sheets = ss.getSheets();
// make an empty array to hold your fetched blobs
const blobs = [];
for (let i = 0; i < sheets.length; i += 1) {
// Convert individual worksheets to PDF
const response = UrlFetchApp.fetch(url + exportOptions + sheets[i].getSheetId(), {
headers: {
Authorization: `Bearer ${token}`,
},
});
// convert the response to a blob and store in our array
blobs[i] = response.getBlob().setName(`${sheets[i].getName()}.pdf`);
}
// create new blob that is a zip file containing our blob array
const zipBlob = Utilities.zip(blobs).setName(`${ss.getName()}.zip`);
// optional: save the file to the root folder of Google Drive
DriveApp.createFile(zipBlob);
// Define the scope
Logger.log(`Storage Space used: ${DriveApp.getStorageUsed()}`);
// If allowed to send emails, send the email with the PDF attachment
if (MailApp.getRemainingDailyQuota() > 0)
GmailApp.sendEmail(email, subject, body, {
htmlBody: body,
attachments: [zipBlob],
});
}
ستقوم وظيفة Google Script بتحويل كل من أوراق عمل جدول بيانات Google إلى ملف PDF ، وتضغط جميع ملفات PDF في ملف مضغوط ويرسلها إلى عنوان بريد إلكتروني مخصص. يمكنك إرسال عناوين بريد إلكتروني متعددة أيضًا – فقط افصل كل منها باستخدام فاصلة.
ترسل الطريقة حاليًا جميع أوراق جدول البيانات في ملف zip ولكن يمكنك أيضًا تحديد معرف ورقة مع &gid
المعلمة لإرسال ورقة معينة فقط. على سبيل المثال ، لإرسال الورقة الأولى ، يمكنك تعيين ملف gid=0
وهلم جرا.
تحويل ورقة Google كاملة إلى ملف PDF
يقوم المقتطف أعلاه بتحويل الأوراق الفردية إلى ملفات PDF منفصلة ولكن هناك أيضًا طريقة لتحويل جدول البيانات بأكمله إلى ملف PDF واحد. في هذه الحالة ، استبدل guid=
مع id=SS_ID
(معرف جدول البيانات) أو إجراء التحويل باستخدام Driveapp كما هو موضح هنا.
البريد الإلكتروني جدول بيانات Google كـ PDF
function emailGoogleSpreadsheetAsPDF() {
// Send the PDF of the spreadsheet to this email address
var email = "amit@labnol.org";
// Get the currently active spreadsheet URL (link)
var ss = SpreadsheetApp.getActiveSpreadsheet();
// Subject of email message
var subject = "PDF generated from spreadsheet " + ss.getName();
// Email Body can be HTML too
var body =
"Sent via <a href='https://workspace.google.com/marketplace/app/email_spreadsheets/431723916752'>Email Spreadsheets</a>";
var blob = DriveApp.getFileById(ss.getId()).getAs("application/pdf");
blob.setName(ss.getName() + ".pdf");
// If allowed to send emails, send the email with the PDF attachment
if (MailApp.getRemainingDailyQuota() > 0)
GmailApp.sendEmail(email, subject, body, {
htmlBody: body,
attachments: [blob],
});
}