تقنية

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


يمكنك إعداد مهمة cron في Google Drive باستخدام Google Apps Script التي سترسل أي جدول بيانات Google، أو أي مستند أو ملف آخر في Drive، إلى عنوان بريد إلكتروني واحد أو أكثر في وقت محدد. يمكنك ضبط المشغل المستند إلى الوقت ليتم تشغيله أسبوعيًا ويوميًا وكل ساعة وجداول زمنية متكررة أخرى.

يلعب ;

يوضح هذا المثال كيفية إرسال جدول بيانات Google إلى عنوان البريد الإلكتروني المحدد تلقائيًا كملف PDF يوميًا. يقوم Google Script بتحويل جدول بيانات Google إلى ملف PDF ويرسله إلى عنوان بريد إلكتروني آخر باستخدام حساب Gmail الخاص بك. يمكنك تخصيص مخرجات PDF بشكل أكبر – مثل إزالة خطوط الشبكة، وإخفاء الصفوف المجمدة، والتغيير إلى الوضع الأفقي، وما إلى ذلك عن طريق تعيين معلمات التصدير الصحيحة.

تحويل وإرسال جداول بيانات جوجل بالبريد الإلكتروني

ال أرسل جدول بيانات جوجل بالبريد الإلكتروني يمكن للوظيفة الإضافية تحويل جداول البيانات تلقائيًا وإرسالها بالبريد الإلكتروني بتنسيقات PDF أو CSV أو Microsoft Excel (xlsx). يمكنه تحويل جدول البيانات بأكمله أو الأوراق الفردية.

يمكن للإصدار المميز من الوظيفة الإضافية إرسال الأوراق المحولة تلقائيًا عبر البريد الإلكتروني على ملف الجدول الزمني المتكرر (مثل كل ساعة، يوميا، أسبوعيا أو شهريا). يمكنك أيضًا إعداد جداول إرسال متعددة وإرسال الورقة بالبريد الإلكتروني تلقائيًا إلى مجموعة مختلفة من الإيصالات في أوقات مختلفة.

برنامج Google النصي لإرسال جداول بيانات Google بالبريد الإلكتروني

إذا لم تتمكن من استخدام الوظيفة الإضافية لجدول بيانات البريد الإلكتروني (لا يسمح بعض مسؤولي Google Apps بالوظائف الإضافية)، فيمكنك كتابة برنامج Google Script الخاص بك لإرسال جدول البيانات بالبريد الإلكتروني كملفات 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 وإرسالها إلى عنوان بريد إلكتروني معين. يمكنك الإرسال إلى عناوين بريد إلكتروني متعددة أيضًا – ما عليك سوى فصل كل منها بفاصلة.

تقوم الطريقة حاليًا بإرسال جميع أوراق جدول البيانات في ملف ZIP ولكن يمكنك أيضًا تحديد معرف الورقة باستخدام ملف &gid المعلمة لإرسال ورقة معينة بالبريد الإلكتروني فقط. على سبيل المثال، لإرسال الورقة الأولى، يمكنك تعيين gid=0 وهكذا.

تحويل ورقة جوجل كاملة إلى ملف 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],
    });
}



Source link

اترك تعليقاً

لن يتم نشر عنوان بريدك الإلكتروني. الحقول الإلزامية مشار إليها بـ *

زر الذهاب إلى الأعلى