25. التنفيذ التلقائي مع بيانات Excel
عندما تكون البيانات متوفرة في Excel على النحو الموضح في ارتباطات بيانات Excel، يمكنك استخدام الدوال UpdateChart
وPresentationFromTemplate
للتحكم برمجيًا باستخدام هذه البيانات.
باستخدام UpdateChart
، يمكنك استبدال ورقة بيانات مخطط محدد بالبيانات الخاصة بك. مع PresentationFromTemplate
، يمكنك استخدام البيانات الخاصة بك لإنشاء عرض تقديمي جديد يستند إلى قالب PowerPoint يتضمن مخططات think-cell مرتبطة بنطاقات بيانات في Excel.
واجهة كلتا الدالتين مدمجة في نموذج التنفيذ التلقائي لبرنامج Office، بحيث يمكن الوصول إليها من أي لغة تستخدمها لبرمجة Office، مثل Visual Basic for Applications أو C#.
نقطة الدخول إلى think-cell هي كائن الوظيفة الإضافية think-cell. يمكن الوصول إليه عبر المجموعة Application.COMAddIns
. دائمًا ما تكون عمليات الاستدعاء في think-cell متأخرة الربط. راجع قاعدة معارف Microsoft للحصول على توضيح:
http://support.microsoft.com/kb/245115
لذلك، يكون نوع كائن الوظيفة الإضافية think-cell هو Object
ببساطة، ولا توجد مكتبة نوع أو مرجع للإضافة. ما عليك سوى الحصول على الكائن، وستكون مستعدًا لإجراء عمليات استدعاء:
Dim tcaddin As Object
Set tcaddin = Application.COMAddIns("thinkcell.addin").Object
- 25.1
- تحديث المخطط
- 25.2
- عرض تقديمي من قالب
25.1 تحديث المخطط
25.1.1 التوقيع
tcaddin.UpdateChart( _
pres As PowerPoint.Presentation, _
strName As String, _
rgData As Excel.Range, _
bTransposed As Boolean _
)
25.1.2 الوصف
عند استدعائها من Excel، تقوم هذه الدالة بتحديث المخطط strName
في pres
باستخدام الأرقام المضمنة في rgData
. يجب أن يكون النطاق rgData
مطابقًا للتخطيط الموضح في إنشاء مخطط من Excel.
لاحظ أن كائن العرض التقديمي pres
يمكن استخدامه أيضًا للرجوع إلى نطاق شريحة في عرض تقديمي. لمزيد من المعلومات، راجع:
http://msdn.microsoft.com/en-us/vba/powerpoint-vba/articles/slide-object-powerpoint
تتم مطابقة اسم المخطط strName
دون حساسية لحالة الأحرف. يجب أن يكون قد تم تعيينه مسبقًا في PowerPoint باستخدام عنصر تحكم الخاصية اسم UpdateChart على النحو الموضح في مقدمة حول التنفيذ التلقائي. يجب أن يكون اسم المخطط فريدًا ضمن العرض التقديمي أو نطاق الشرائح المعرفة من pres
.
إذا كان المخطط مرتبطًا بأي نطاق بيانات Excel عند استدعاء هذه الدالة، يتم قطع الارتباط. بعد ذلك لن يتم ربط المخطط بأي نطاق Excel.
25.1.3 مثال
لاستخدام هذا النموذج، في نافذة Visual Basic for Applications في Excel، انتقل إلى أدوات، ثم المراجع، وقم بإضافة مكتبة كائنات Microsoft PowerPoint.
' When Option Explicit appears in a file, you must
' explicitly declare all variables using the Dim
' or ReDim statements. If you attempt to use an
' undeclared variable name, an error occurs at
' compile time.
' Use Option Explicit to avoid incorrectly typing
' the name of an existing variable or to avoid
' confusion in code where the scope of the
' variable is not clear. If you do not use the
' Option Explicit statement, all undeclared
' variables are of Object type.
' http://msdn.microsoft.com/en-us/
' library/y9341s4f%28v=vs.80%29.aspx
Option Explicit
Sub UpdateChart_Sample()
' Get the range containing the new data
Dim rng As Excel.Range
Set rng = ActiveWorkbook.Sheets("Sheet1").Range("A1:D5")
' Get the think-cell add-in object
Dim tcaddin As Object
Set tcaddin = Application.COMAddIns("thinkcell.addin").Object
' Get a PowerPoint instance. Hold on to this
' object as long as you want to access the
' generated presentations. There can only be a
' single PowerPoint instance. If there is no
' PowerPoint running, one will be started.
' Otherwise the existing one is used.
Dim ppapp As Object
Set ppapp = New PowerPoint.Application
Dim pres As PowerPoint.Presentation
' PowerPoint window visible
' Set pres = ppapp.Presentations.Open( _
' Filename:="c:\\template.pptx", _
' Untitled:=msoTrue)
' PowerPoint window invisible
Set pres = ppapp.Presentations.Open( _
Filename:="c:\\template.pptx", _
Untitled:=msoTrue, _
WithWindow:=msoFalse)
' The name "Chart1" must have been
' previously assigned to the chart using
' the control in the floating toolbar.
' The final argument indicates whether
' the data range is transposed or not.
Call tcaddin.UpdateChart(pres, "Chart1", rng, False)
' Save the updated presentation
pres.SaveAs ("c:\\template_updated.pptx")
pres.Close
ppapp.Quit
End Sub
يعرض المثال التالي كيفية استخدام UpdateChart
من C#.
using Excel = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Office = Microsoft.Office.Core;
// Open the Solution Explorer > right-click the project file > "Add Reference..." button and add he following references:
// .NET tab > Microsoft.Office.Interop.Excel 12.0.0.0
// .NET tab > Microsoft.Office.Interop.PowerPoint 12.0.0.0
// COM tab > Microsoft Office 14.0 Object Library
namespace ConsoleApplication_UpdateChart
{
class Program
{
static void Main()
{
Excel.Application xlapp = new Excel.Application();
xlapp.Visible = true;
Excel.Workbook workbook = xlapp.Workbooks.Add(1);
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Sheets[1];
worksheet.Cells[3, 1] = "Series 1";
worksheet.Cells[3, 2] = 1;
worksheet.Cells[3, 3] = 2;
worksheet.Cells[3, 4] = 3;
PowerPoint.Application ppapp = new PowerPoint.Application();
PowerPoint.Presentation presentation =
ppapp.Presentations.Open("C://template.pptx", Office.MsoTriState.msoFalse, Office.MsoTriState.msoTrue);
object[] aobjArg = new object[] { (object)presentation, "Chart1", worksheet.get_Range("A1", "D3"), false };
Office.COMAddIn comaddin = xlapp.COMAddIns.Item("thinkcell.addin");
object objAddIn = comaddin.Object;
objAddIn.GetType().InvokeMember("UpdateChart", System.Reflection.BindingFlags.InvokeMethod, null, objAddIn, aobjArg);
presentation.SaveAs("C://template_updated.pptx");
presentation.Close();
ppapp.Quit();
workbook.Close(false);
xlapp.Quit();
}
}
}
25.2 عرض تقديمي من قالب
25.2.1 التوقيع
tcaddin.PresentationFromTemplate( _
wb As Excel.Workbook, _
strTemplate As String, _
ppapp As PowerPoint.Application _
) As PowerPoint.Presentation
25.2.2 الوصف
عند استدعائها من Excel، تقوم هذه الدالة بتطبيق أي ارتباطات بيانات في wb
على القالب الذي يحمل اسم الملف strTemplate
. تكون النتيجة عرضًا تقديميًا جديدًا داخل مثيل PowerPoint ppapp
.
قد يكون strTemplate
مسارًا كاملاً أو مسارًا نسبيًا يتم التعامل معه بعد ذلك باعتباره نسبيًا إلى موقع ملف مصنف Excel wb
.
يتم تحديث جميع المخططات في strTemplate
المرتبطة بمصنف Excel wb
(بصرف النظر عما إذا كان قد تم تعيينها على التحديث التلقائي أم لا). ثم يتم قطع ارتباطات البيانات الخاصة بها لمنع إجراء تغييرات إضافية على هذه المخططات.
يتم ترك المخططات في strTemplate
المرتبطة بمصنفات Excel بخلاف wb
دون تغيير وتظل مرتبطة، لذلك يمكن تحديث الارتباطات من مصنفات Excel متعددة من خلال حفظ نتيجة هذه الدالة باسم قالب جديد ثم استدعاء هذه الدالة مرة أخرى مع المصنف التالي.
إذا كنت تريد التحكم في ألوان مقاطع المخطط باستخدام ارتباط Excel، يمكنك تعيين نظام الألوان على استخدام تعبئة Excel بصورة إضافية (راجع نظام الألوان). بالمثل، للتحكم في تنسيق الأرقام باستخدام ارتباط Excel، قم بتعيينه على استخدام تنسيق Excel (راجع تنسيق الأرقام). تأكد من تعيين لون الخلفية وتنسيق الأرقام للخلايا المعنية في Excel قبل استدعاء PresentationFromTemplate
.
25.2.3 مثال
لاستخدام هذا النموذج، في نافذة Visual Basic for Applications في Excel، انتقل إلى أدوات، ثم المراجع، وقم بإضافة مكتبة كائنات Microsoft PowerPoint.
' When Option Explicit appears in a file, you must
' explicitly declare all variables using the Dim
' or ReDim statements. If you attempt to use an
' undeclared variable name, an error occurs at
' compile time.
' Use Option Explicit to avoid incorrectly typing
' the name of an existing variable or to avoid
' confusion in code where the scope of the
' variable is not clear. If you do not use the
' Option Explicit statement, all undeclared
' variables are of Object type.
' http://msdn.microsoft.com/en-us/
' library/y9341s4f%28v=vs.80%29.aspx
Option Explicit
Sub PresentationFromTemplate_Sample()
' Get the range to modify. It is more efficient
' to do this once rather than within the loop.
Dim rng As Excel.Range
Set rng = ActiveWorkbook.Sheets("Sheet1").Cells(3, 2)
' Get the think-cell add-in object
Dim tcaddin As Object
Set tcaddin = Application.COMAddIns("thinkcell.addin").Object
' Get a PowerPoint instance. Hold on to this
' object as long as you want to access the
' generated presentations. There can only be a
' single PowerPoint instance. If there is no
' PowerPoint running, one will be started.
' Otherwise the existing one is used.
Dim ppapp As Object
Set ppapp = New PowerPoint.Application
Dim i As Integer
For i = 1 To 10
' Modify the range value.
' Note: Avoid selecting the cell prior to
' changing it. It is very slow and has
' undesirable side-effects.
' BAD:
' rng.Select
' ActiveWindow.Selection.Value = 0
rng.Value = i
' Generate a new presentation based on the
' linked template.
Dim pres As PowerPoint.Presentation
Set pres = tcaddin.PresentationFromTemplate( _
Excel.ActiveWorkbook, "template.pptx", ppapp)
' If you want to modify the new presentation
' before saving it this is the place to do it.
' Save the new presentation
pres.SaveAs "c:\\output" & i & ".pptx"
' Explicitly close the presentation when we
' are done with it to free its memory.
' Letting the object go out of scope is not
' sufficient.
pres.Close
Next
End Sub