আপনার রূপান্তরিত কোডে ত্রুটি ঠিক করুন

ম্যাক্রো কনভার্টার অ্যাড-অন বেশিরভাগ রূপান্তর প্রক্রিয়া স্বয়ংক্রিয় করে, তবে আপনার কোড চূড়ান্ত করার জন্য আপনাকে কিছু API এবং অন্যান্য আইটেমগুলিতে সমন্বয় করতে হতে পারে।

আপনার প্রোজেক্টে যোগ করা অ্যাপস স্ক্রিপ্ট ফাইল (GS ফাইল) বুঝতে, বিভিন্ন ধরণের ত্রুটি ব্যাখ্যা করতে এবং ত্রুটিগুলি কীভাবে ঠিক করতে হয় তা শিখতে এই নির্দেশিকাটি ব্যবহার করুন।

আপনার প্রোজেক্টে যোগ করা অ্যাপস স্ক্রিপ্ট ফাইলগুলি বুঝুন

আপনার অ্যাপস স্ক্রিপ্ট প্রজেক্টে অতিরিক্ত GS ফাইল যোগ করা হয়েছে যাতে সাহায্য করা যায়:

  • অ্যাপস স্ক্রিপ্টে বিদ্যমান নেই এমন VBA ধ্রুবক এবং মানগুলি সংজ্ঞায়িত করুন।
  • রূপান্তরিত না হওয়া API গুলি বাস্তবায়ন করুন।
  • ভেরিয়েন্টগুলি সমাধান করুন।

আপনার অ্যাপস স্ক্রিপ্ট প্রজেক্টে নিম্নলিখিত GS ফাইলগুলি যোগ করা হয়েছে:

  • Library.gs
  • Unimplemented_constructs.gs
  • Variant_resolutions.gs

Library.gs

সাধারণভাবে, আপনাকে library.gs ফাইলে কিছু পরিবর্তন করার প্রয়োজন নেই।

library.gs ফাইলটি আপনার VBA কোডে ব্যবহৃত ফাংশন এবং ধ্রুবকগুলিকে সংজ্ঞায়িত করে যা Apps Script-এ বিদ্যমান নেই। এটি নতুন Apps Script কোডটিকে আপনার VBA কোডের সাথে আরও ভালোভাবে সাদৃশ্যপূর্ণ করতে সাহায্য করে। এছাড়াও, library.gs ফাইল থেকে ফাংশন বা ধ্রুবক ব্যবহার করার সময় আপনাকে প্রতিবার সংজ্ঞা পুনরাবৃত্তি করতে হবে না।

Unimplemented_constructs.gs

unimplemented_constructs.gs ফাইলটি এমন কনস্ট্রাক্ট বা API গুলিকে ঠিকানা দেয় যা ম্যাক্রো কনভার্টার দ্বারা রূপান্তর করা যায়নি। আপনার কোডটি উদ্দেশ্য অনুসারে কাজ করার জন্য আপনাকে সম্ভবত এই ফাইলটি পরিবর্তন করতে হবে।

উদাহরণ: Window.Activate()

নিচে Window.Activate() নামক একটি অসমর্থিত API-এর উদাহরণ দেওয়া হল। ম্যাক্রো কনভার্টারটি একই নামের একটি নতুন অ্যাপস স্ক্রিপ্ট ফাংশন তৈরি করে এবং unimplemented_constructs.gs ফাইলে এটি সংজ্ঞায়িত করে। যেহেতু VBA ফাংশনটি সমর্থিত নয়, তাই নতুন অ্যাপস স্ক্রিপ্ট ফাংশনটি একটি ব্যতিক্রম দেয়।

VBA কোডে মূল API যেখানেই ব্যবহৃত হয়েছিল, সেখানেই রূপান্তরিত অ্যাপস স্ক্রিপ্ট কোডে নতুন ফাংশনটি যোগ করা হয়েছে।

যদি আপনি মূল API-এর আচরণ পুনরায় তৈরি করার জন্য কোনও সমাধান খুঁজে পান, তাহলে আপনাকে কেবল unimplemented_constructs.gs ফাইলে ফাংশনের সংজ্ঞা আপডেট করতে হবে। একবার সেখানে ফাংশনটি সংজ্ঞায়িত হয়ে গেলে, এটি আপনার অ্যাপস স্ক্রিপ্ট প্রজেক্টে ফাংশনটি যেখানেই প্রদর্শিত হবে সেখানে প্রযোজ্য হবে।

কোডে উদাহরণটি এখানে দেওয়া হল:

মূল VBA কোড

Window.activate()

রূপান্তরিত অ্যাপস স্ক্রিপ্ট কোড, ইন-লাইন যোগ করা হয়েছে

_api_window_activate();

unimplemented_constructs.gs ফাইলে ফাংশনের সংজ্ঞা যোগ করা হয়েছে।

/**
 * Could not convert window.activate API. Please add relevant code in the
 * following function to implement it.
 * This API has been used at the following locations in the VBA script.
 *     module1 : line 3
 *
 * We couldn't find an equivalent API in Apps Script for this VBA API. Please
 * reconsider if this function call is critical, otherwise consider implementing
 * it in a different way.
 */
function _api_window_activate(CallingObject) {
  ThrowException("API window.activate not supported yet.");
}

Variant_resolutions.gs

যদি কোনও অবজেক্টের ধরণ নির্ধারণ করা না যায়, তাহলে variant_resolutions.gs ফাইলটি আপনার অ্যাপস স্ক্রিপ্ট প্রজেক্টে যোগ করা হয়। এটি একাধিক কারণে ঘটতে পারে, যেমন একাধিক রিটার্ন টাইপের API থাকা অথবা অবজেক্টটিকে নিজেই একটি ভেরিয়েন্ট হিসেবে ঘোষণা করা।

ম্যাক্রো কনভার্টার এই ফাইলে __handle_resolve_<api>() নামে একটি নতুন ফাংশন যোগ করে যা প্রশ্নবিদ্ধ API কে প্রতিস্থাপন করে এবং অবজেক্টের ধরণ নির্ধারণে সহায়তা করে।

কিছু ক্ষেত্রে, অবজেক্টের ধরণ ম্যানুয়ালি ঘোষণা করার জন্য আপনাকে __handle_resolve_<api>() ফাংশনটি আপডেট করতে হতে পারে। Unsupported object type দেখুন।

উদাহরণ: name()

VBA-তে অনেক অবজেক্ট টাইপ একটি name() API সংজ্ঞায়িত করে। সাধারণত, Apps Script এর সমতুল্য হল getName() , কিন্তু প্রতিটি অবজেক্ট টাইপের জন্য নয়। একাধিক বিকল্প ঘটনা ঘটতে পারে:

  • বস্তুর সমতুল্য API কে getName() থেকে ভিন্ন কিছু বলা হয়।
  • অবজেক্টটির নাম জানার জন্য অ্যাপস স্ক্রিপ্ট এপিআই নেই।
  • এর সমতুল্য কোনও অ্যাপস স্ক্রিপ্ট অবজেক্ট নেই।

যখন অবজেক্টের ধরণ নির্ধারণ করা না হয়, তখন ম্যাক্রো কনভার্টার variant_resolutions.gs ফাইলে __handle_resolve_name নামে একটি নতুন ফাংশন তৈরি করে।

কোডে উদাহরণটি এখানে দেওয়া হল:

মূল VBA কোড

a = Selection.name

এই ক্ষেত্রে, বর্তমান নির্বাচনের উপর API name() ডাকা হয়। নির্বাচনটি একটি Sheet অবজেক্ট অথবা একটি Shape অবজেক্ট হতে পারে। যদি এটি একটি Sheet অবজেক্ট হয়, তাহলে অনুবাদটি হল getName() , কিন্তু যদি এটি একটি Shape অবজেক্ট হয়, তাহলে Apps Script-এ এর সমতুল্য কোন শব্দ নেই।

রূপান্তরিত অ্যাপস স্ক্রিপ্ট কোড, ইন-লাইন যোগ করা হয়েছে

a = __handle_resolve_name({}, getActiveSelection(), {});

নিচের __handle_resolve_name() ফাংশনটি বিভিন্ন ধরণের অবজেক্টের সমাধানের জন্য variant_resolution.gs ফাইলে যোগ করা হয়েছে। ফাংশনটি অবজেক্টের ধরণ পরীক্ষা করে, তারপর যদি এটি সমর্থিত হয় তবে getName() ব্যবহার করে, অথবা যদি getName() সমর্থিত না হয় তবে একটি ত্রুটি ছুঁড়ে দেয়।

variant_resolution.gs ফাইলে ফাংশনের সংজ্ঞা যোগ করা হয়েছে।

function __handle_resolve_name(ExecutionContext, CallingObject, params_map) {
  var found_api_variant = false;
  var return_value;
  if (String(CallingObject) == "Sheet") {
    if (!ExecutionContext.isLhs) {
      return_value = CallingObject.getName();
      found_api_variant = true;
    }
  }
  if (CallingObject instanceof ChartInSheet) {
    if (!ExecutionContext.isLhs) {
      return_value = CallingObject.getName();
      found_api_variant = true;
    }
  }
  if (!found_api_variant) {
    ThrowException("API .name not supported yet.");
  }
  return return_value;
}

ত্রুটি খুঁজুন

যখন আপনি রূপান্তরিত অ্যাপস স্ক্রিপ্ট কোডে কোনও ত্রুটির সম্মুখীন হন, তখন বার্তাটি ত্রুটির ধরণ এবং তার অবস্থান নির্দিষ্ট করে। ত্রুটি বার্তার ফর্ম্যাটটি আপনি কোন অ্যাপস স্ক্রিপ্ট রানটাইম ব্যবহার করছেন তার উপর নির্ভর করে।

যদি আপনি ডিফল্ট V8 রানটাইমে থাকেন, তাহলে আপনি একটি ত্রুটি দেখতে পাবেন যা দেখতে নিচের মতো:

_api_windows_active (unimplemented_constructs:2:3)

এর অর্থ হল ত্রুটিটি unimplemented_constructs.gs ফাইলের লাইন ২, অক্ষর ৩-এ অবস্থিত।

যদি আপনি অবচিত Rhino রানটাইমে থাকেন, তাহলে আপনি একটি ত্রুটি দেখতে পাবেন যা নিম্নলিখিতটির মতো দেখাচ্ছে:

unimplemented_constructs:2 (_api_windows_active)

এর অর্থ হল ত্রুটিটি লাইন ২-এর unimplemented_constructs.gs ফাইলে অবস্থিত।

ত্রুটির ধরণ

উপরে বর্ণিত unimplemented_constructs.gs এবং variant_resolution.gs ফাইলগুলিতে আপনি যে বেশিরভাগ ত্রুটির সম্মুখীন হন তার সমাধান করতে পারেন।

আপনি যে ধরণের ত্রুটির সম্মুখীন হতে পারেন তার মধ্যে রয়েছে:

বাস্তবায়িত না হওয়া API

একটি আনইমপ্লিমেন্টেড এপিআই হলো এমন একটি এপিআই যা ম্যাক্রো কনভার্টার VBA থেকে অ্যাপস স্ক্রিপ্টে রূপান্তর করতে পারে না এবং এপিআইয়ের জন্য কোনও পরিচিত সমাধান নেই।

অবাস্তবায়িত API গুলি সাধারণত খালি ফাংশন হিসেবে যোগ করা হয়—কখনও কখনও খালি স্বাক্ষর সহ— unimplemented_constructs.gs ফাইলে। যদি অবজেক্টের ধরণ নির্ধারণ করা না যায়, তাহলে অবাস্তবায়িত API টি variant_resolution.gs ফাইলে যোগ করা যেতে পারে।

রূপান্তরের আগে আপনার তৈরি করা সামঞ্জস্য প্রতিবেদনে, এই API-টিকে "আরও তদন্তের প্রয়োজন" হিসাবে লেবেল করা হয়েছে।

যদি আপনি আপনার ফাইল রূপান্তর করার আগে আপনার VBA কোডে এই ধরণের API ঠিক না করেন, তাহলে Apps Script প্রকল্পে এটি কীভাবে প্রদর্শিত হবে তা এখানে দেখুন:

/**
* Could not convert . Please add relevant code in the following
* function to implement it.
* This API has been used at the following locations in the VBA script.
*      : 
* We couldn't find an equivalent API in Apps Script for this VBA API. Please
* reconsider if this function call is critical, otherwise consider implementing
* it in a different way.
* @param param1 {}
* @param param2 {}
* ...
* @return {}
*/
function _api_<API_name>(param1, param2, ....) {
  ThrowException("API  not supported yet.");
}

বাস্তবায়িত না হওয়া API ত্রুটিগুলি ঠিক করুন

বিদ্যমান অ্যাপস স্ক্রিপ্ট এপিআই বা জেএস লাইব্রেরি দিয়ে বাস্তবায়িত না হওয়া এপিআই নির্ধারণ করুন। এটি করতে, এই পদক্ষেপগুলি অনুসরণ করুন:

  1. ত্রুটির স্থানে রূপান্তরিত অ্যাপস স্ক্রিপ্ট কোডটি খুলুন। ত্রুটি খুঁজুন দেখুন।
  2. ফাংশনের উপরে, যোগ করা মন্তব্যটি পড়ুন। কিছু ক্ষেত্রে, মন্তব্যটি অ্যাপস স্ক্রিপ্টে API কীভাবে বাস্তবায়ন করতে হয় তা পরামর্শ দেয়।
  3. যদি আপনি অ্যাপস স্ক্রিপ্টে API বাস্তবায়নের কোন উপায় খুঁজে না পান, তাহলে আপনার কোড থেকে এটি সরিয়ে ফেলার কথা বিবেচনা করুন।
  4. যদি আপনি কোনও সমাধান খুঁজে না পান অথবা আপনার কোড থেকে এই APIটি সরিয়ে না ফেলেন এবং আপনার ম্যাক্রো এই ত্রুটিটি ফেলে দেয়, তাহলে আপনি এই ম্যাক্রোটিকে রূপান্তর করতে পারবেন না।

বাস্তবায়িত না হওয়া API ত্রুটির উদাহরণ

এখানে বাস্তবায়িত না হওয়া API পরিস্থিতির উদাহরণ এবং সেগুলি কীভাবে ঠিক করবেন তা দেওয়া হল:

  • এর সমতুল্য কোনও অ্যাপস স্ক্রিপ্ট নেই : Chart.Protect এর জন্য একটি পরোক্ষ সমাধান দেখায়, এটি এমন একটি API যা অ্যাপস স্ক্রিপ্টে বিদ্যমান নেই।
  • একটি অজানা অবজেক্ট টাইপ : কীভাবে একটি ভেরিয়েবল অবজেক্ট টাইপ পরিচালনা করতে হয় এবং কীভাবে একটি অসমর্থিত অবজেক্ট টাইপ বাস্তবায়ন করতে হয় যা অ্যাপস স্ক্রিপ্টে পুনরায় তৈরি করা যেতে পারে তা দেখায়।
উদাহরণ ১: কোনও সমতুল্য অ্যাপস স্ক্রিপ্ট বা অজানা API নেই

এই উদাহরণে, Chart.Protect স্বয়ংক্রিয়ভাবে রূপান্তরিত হয়নি কারণ Google Sheets-এ চার্ট সুরক্ষিত করার কোনও উপায় নেই।

/**
* Could not convert chart.protect API. Please add relevant code in the following
* function to implement it.
*
* This API has been used at the following locations in the VBA script.
*     sheet1 : line 3
* You can use the following Apps Script APIs to convert it.
*
* Comments : Auto conversion of Chart.Protect is not supported yet. If the API is
* critical for the workflow the user can implement the unimplemented handler
* method in the generated code, else comment out the throw statement.
*
* @param {Object} CallingObject represents the parent object using which the API
* has been called.
* @param {string} Password
* @param {boolean} DrawingObjects
* @param {boolean} Contents
* @param {boolean} Scenarios
* @param {boolean} UserInterfaceOnly
*
*/
function _api_chart_protect(
   CallingObject, Password, DrawingObjects, Contents, Scenarios,
   UserInterfaceOnly) {
 ThrowException('API chart.protect not supported yet.');
}
যদিও আপনি একটি চার্ট সুরক্ষিত করতে পারবেন না, আপনি চার্টের ডেটা পরিসর সুরক্ষিত করতে পারেন যাতে ডেটা পরিবর্তন করা না যায়।

পরিসর রক্ষার একটি নমুনা বাস্তবায়ন নীচে দেখানো হয়েছে:
/**
* Could not convert chart.protect API. Please add relevant code in the following
* function to implement it.
* This API has been used at the following locations in the VBA script.
*     sheet1 : line 3
*
* You can use the following Apps Script APIs to convert it.
* Comments : Auto conversion of Chart.Protect is not supported yet. If the API
* is critical for the workflow the user can implement the unimplemented handler
* method in the generated code, else comment out the throw statement.
*
* @param {Object} CallingObject represents the parent object using which the API
* has been called.
* @param {string} Password
* @param {boolean} DrawingObjects
* @param {boolean} Contents
* @param {boolean} Scenarios
* @param {boolean} UserInterfaceOnly
*/
function _api_chart_protect(
  CallingObject, Password, DrawingObjects, Contents, Scenarios, UserInterfaceOnly) {
var ranges = CallingObject.getChart().getRanges();
for (var i = 0; i < ranges.length; i++) {
  // Note that this does not lock the range for the document owner.
  ranges[i].protect();
}
}
উদাহরণ ২: অসমর্থিত বস্তুর ধরণ

যখন অবজেক্টের ধরণ অজানা থাকে, তখন বাস্তবায়িত না হওয়া API ত্রুটিটি variant_resolution.gs ফাইলে যোগ করা হয়। নিম্নলিখিত উদাহরণটি উপরের VBA name() API উদাহরণে প্রসারিত হয়। variant_resolution.gs দেখুন।

এই উদাহরণে, আপনি শিখবেন:

  1. variant_resolution.gs ফাইলে name() API কীভাবে একটি নতুন ফাংশনে রূপান্তরিত হয়
  2. রূপান্তরিত কোডে নতুন ফাংশনটি কীভাবে ডাকা হয়
  3. অ্যাপস স্ক্রিপ্টে অসমর্থিত অবজেক্ট টাইপ CommandBar জন্য কীভাবে একটি সমাধান তৈরি করবেন

১. যেহেতু রূপান্তরিত কোডটি সঠিক বস্তুর ধরণ নির্ধারণ করতে পারে না যে name() ব্যবহার করে কল করা হয়েছে, তাই ম্যাক্রো কনভার্টারটি __handle_resolve_name নামে একটি নতুন ফাংশন তৈরি করে, যা নীচে দেখানো হয়েছে।

function __handle_resolve_name(ExecutionContext, CallingObject, params_map) {
 var found_api_variant = false;
 var return_value;
  if (String(CallingObject) == "Sheet") {
    if (!ExecutionContext.isLhs) {
      return_value = CallingObject.getName();
      found_api_variant = true;
    }
  }
  if (CallingObject instanceof ChartInSheet) {
    if (!ExecutionContext.isLhs) {
      return_value = CallingObject.getName();
      found_api_variant = true;
    }
  }
  if (!found_api_variant) {
    ThrowException('API .name not supported yet.');
  }
  return return_value;
}

২. ধরুন VBA কোডটি একটি PrintName() ফাংশন সংজ্ঞায়িত করে যা name() API কল করে। VBA কোডটি নিচে দেখানো হল:

‘Defining a function that prints the name of the object in parameter
Sub PrintName(obj as Variant)
  Debug.Print obj.Name
End Sub
যেহেতু `name()` একটি ভেরিয়েবল অবজেক্টে কল করা হয়, তাই রূপান্তরিত কোডটি রূপান্তরের সময় অবজেক্টের ধরণটি জানে না। রূপান্তরিত অ্যাপস স্ক্রিপ্ট কোড `__handle_resolve_name` ফাংশনটিকে কল করবে:
function PrintName(obj) {
  Logger.log(_handle_resolve_name(obj));
}

৩. ধরুন আপনার VBA কোডটি CommandBar অবজেক্ট টাইপের PrintName() ফাংশনটিকে কল করে। VBA কোডটি নিচে দেখানো হল:

PrintName Application.CommandBars.item("Standard")
অ্যাপস স্ক্রিপ্টে CommandBar সমর্থিত নয় এবং ফলস্বরূপ, উপরের VBA কোডে ব্যবহৃত দুটি পদ্ধতিও সমর্থিত নয়।
  • Application.CommandBars() : VBA তে, এটি সমস্ত CommandBar অবজেক্টের একটি তালিকা প্রদান করে।
  • CommandBars.item() : VBA তে, এটি একটি নির্দিষ্ট CommandBar অবজেক্ট ফেরত দেয়।
যেহেতু এই অবজেক্ট টাইপটি অ্যাপস স্ক্রিপ্টে সমর্থিত নয়, তাই রূপান্তরিত কোডটি `unimplemented_constructs.gs` ফাইলে নিম্নলিখিত ফাংশন তৈরি করে যা আপনাকে সংজ্ঞায়িত করতে হবে।
  • _api_application_commandbars()
  • _api_commandbars_item()
ফাংশনগুলো নিচের চিত্রের মতো রূপান্তরিত কোডে ডাকা হয়:
PrintName(_api_commandbars_item(_api_application_commandbars(), "Standard")))

Heres how the new functions are added to the unimplemented_construct.gs file:

function _api_application_commandbars(CallingObject) {
  ThrowException('API application.commandbars not supported yet.');
}
function _api_commandbars_item(CallingObject, index) {
  ThrowException('API commandbars.item not supported yet.');
}

নতুন ফাংশনগুলি কাজ করতে, নিম্নলিখিত পদক্ষেপগুলি গ্রহণ করুন:

৩.১ একটি নতুন অবজেক্ট টাইপ সংজ্ঞায়িত করুন যা CommandBars কার্যকারিতা তৈরি করে এবং VBA তে বিদ্যমান CommandBars অনুরূপ একটি নতুন সংগ্রহ তৈরি করে।

৩.২ নতুন অবজেক্ট টাইপের জন্য একটি getName() পদ্ধতি যোগ করুন।

নিচের কোডে ধাপ ৩.১ এবং ৩.২ দেখানো হয়েছে। মেনু অবজেক্টগুলি একটি নতুন অবজেক্ট টাইপ হিসেবে তৈরি করা হয়েছে যা CommandBars এর আচরণ অনুকরণ করে।

// Our Implementation of CommandBar using Menu objects.

function CommandBar(name) {
  this.name = name;
  // Create a menu object to represent the commandbar.
  this.menu = SpreadsheetApp.getUi().createMenu(name);
  // Create methods for retrieving or updating the name of the object
  this.getName = function() {
    return this.name;
  };
  this.updateName = function(name) {
    this.name = name;
  };
  // ========================================================================
  // Implement other methods of CommandBar objects that are used in the script.
  // =====================================================================
  return this;
}
// Our implementation of the collection of CommandBars that exists in VBA
function CommandBars() {
  this.commandBars = [];
  this.getCommandBar = function(name) {
    for (var i = 0; i < this.commandBars.length; i++) {
      if (!this.commandBars[i].getName() == name) {
        return this.commandBars[i];
      }
    }
    // No commandBar with the name exists, create a new one and return.
    var commandBar = new CommandBar(name);
    this.commandBars.push(commandBar);
    return commandBar;
  };
  return this;
}
// Create a global object that represents CommandBars collection.
var GlobalCommandBars = new CommandBars();

৩.৩ নতুন অবজেক্ট টাইপ পরিচালনা করার জন্য variant_resolution.gs ফাইলে __handle_resolve_name ফাংশনটি পরিবর্তন করুন। ফাংশনে একটি বিভাগ যোগ করুন, যেমনটি নীচে দেখানো হয়েছে:

function __handle_resolve_name(ExecutionContext, CallingObject, params_map) {
 var found_api_variant = false;
 var return_value;
 if (String(CallingObject) == "Sheet") {
   if (!ExecutionContext.isLhs) {
     return_value = CallingObject.getName();
     found_api_variant = true;
   }
 }
 if (CallingObject instanceof ChartInSheet) {
   if (!ExecutionContext.isLhs) {
     return_value = CallingObject.getName();
     found_api_variant = true;
   }
 }
 // New section added below
 // ========================================================================
 if (CallingObject instanceof CommandBar) {
   objectExtend(params_map, {VALUETOSET: params_map.param0});
   if (ExecutionContext.isLhs) {
     // Call the setter method.
     CallingObject.updateName(params_map.VALUETOSET);
     found_api_variant = true;
   } else {
     // Getter is called, return the commandbar name,
     return_value = CallingObject.getName();
     found_api_variant = true;
   }
 }
 // ========================================================================
 // New section added above
 if (!found_api_variant) {
   ThrowException('API .name not supported yet.');
 }
 return return_value;
}

৩.৪ unimplemented_constructs.gs ফাইলে তৈরি দুটি ফাংশন সংজ্ঞায়িত করুন ( _api_application_commandbars() , _api_commandbars_item() )। এই ধাপটি নিশ্চিত করে যে ফাংশনের মূল কলগুলি কাজ করছে।

//This is straightforward based on the implementation of a CommandBar and the
// CommandBars collection above:
function _api_application_commandbars(CallingObject) {
 return GlobalCommandBars;
}
function _api_commandbars_item(CallingObject, index) {
 return CallingObject.getCommandBar(index);
}

বাস্তবায়িত না হওয়া ভাষা গঠন

একটি কনস্ট্রাক্ট হল কোড ভাষার একটি উপাদান যা এক্সিকিউশন ফ্লো বা ডেটা প্রদর্শন নিয়ন্ত্রণ করে। উদাহরণস্বরূপ, লুপ, লেবেল, ইভেন্ট এবং গোটো। এখানে সমস্ত VBA কনস্ট্রাক্টের একটি তালিকা রয়েছে।

ম্যাক্রো কনভার্টার যে কনস্ট্রাক্টগুলিকে কনভার্ট করতে পারে না সেগুলিকে অবাস্তবায়িত ভাষা কনস্ট্রাক্ট হিসাবে বিবেচনা করা হয়।

যেখানে ম্যাক্রো কনভার্টার নির্ধারণ করে যে একটি অবাস্তবায়িত ভাষা গঠন বিদ্যমান, এটি একটি TODO মন্তব্য সন্নিবেশ করায়।

নিম্নলিখিত VBA গঠনগুলি সমর্থিত নয়:

বাস্তবায়িত না হওয়া ভাষা গঠনের ত্রুটিগুলি ঠিক করুন

  1. আপনার কোডটি আপডেট করুন যাতে আপনার যুক্তি অসমর্থিত ভাষা গঠনের উপর নির্ভর না করে।
  2. ত্রুটির স্থানে রূপান্তরিত অ্যাপস স্ক্রিপ্ট কোডটি খুলুন। ত্রুটি খুঁজুন দেখুন।
  3. কোডের যুক্তির উপর ভিত্তি করে, এটি এমনভাবে আপডেট করুন যাতে অসমর্থিত ভাষা গঠনের প্রয়োজন না হয়।
  4. যদি আপনি অসমর্থিত ভাষা গঠন ছাড়া আপনার কোড পুনর্লিখনের কোনও উপায় খুঁজে না পান, তাহলে আপনি এই ম্যাক্রো রূপান্তর করতে পারবেন না।

বাস্তবায়িত না হওয়া ভাষা গঠন ত্রুটির উদাহরণ

সবচেয়ে সাধারণ অবাস্তবায়িত ভাষা গঠনগুলির মধ্যে একটি হল GoTo স্টেটমেন্ট। আপনি কিছু VBA GoTo স্টেটমেন্টকে লুপ দিয়ে প্রতিস্থাপন করতে পারেন। GoTo স্টেটমেন্টের পরিবর্তে লুপ ব্যবহারের দুটি উদাহরণ নীচে দেওয়া হল।

উদাহরণ ১: GoTo While Loop দিয়ে প্রতিস্থাপন করুন।

মূল VBA কোড
Sub Test()
 a = 0
 start: Debug.Print a
 While a < 100
   a = a + 1
   If a Mod 3 == 0
     Goto start
   End If
 Wend
End Sub
সমতুল্য অ্যাপস স্ক্রিপ্ট কোড
function test() {
 var a = 0;
 start: do {
   console.log(a);
   while (a < 100) {
     a = a + 1;
     if (a % 3 == 0) {
       continue start;
     }
   }
   break start;
 } while (true);
}

উদাহরণ ২: GoTo কে For Loop দিয়ে প্রতিস্থাপন করুন।

মূল VBA কোড
Sub Test()
 a = 0
 For i = 1 to 100
   For j = 1 to 10
     a =a a + 1
     If i + j > 50
       GoTo endLoop
     End If
   Next j
 Next i
 endLoop: MsgBox a
End Sub
সমতুল্য অ্যাপস স্ক্রিপ্ট কোড
function test() {
 var a = 0;
 endLoop: for (var i = 1; i <= 100; i++) {
    for  (var j = 0; j <=10; j++) {
      If (i + j > 50) {
        break endLoop;
      }
    }
 }
 Browser.msgBox(a);
}

   break start;
 } while (true);
}

আংশিকভাবে সমর্থিত API

আংশিকভাবে সমর্থিত API গুলির জন্য, কিছু ইনপুট প্যারামিটার অ্যাপস স্ক্রিপ্টে সমর্থিত এবং কিছু নয়।

উদাহরণস্বরূপ, VBA API legend_position একটি এক্সেল গ্রাফে legend সংজ্ঞায়িত করতে ব্যবহৃত হয়। এটি একাধিক ধরণের ইনপুট মান সমর্থন করে, যার মধ্যে রয়েছে:

  • xlLegendPositionBottom : লেজেন্ডটিকে চার্টের নীচে রাখে।
  • xlLegendPositionCorner : লেজেন্ডটিকে চার্টের কোণে রাখে।
  • xlLegendPositionCustom : চার্টের কাস্টম অবস্থানে লেজেন্ড রাখে।

অ্যাপস স্ক্রিপ্টের একটি সমতুল্য কোড রয়েছে যা কেবলমাত্র কিছু মান সমর্থন করে। নিম্নলিখিত মানগুলি সমর্থিত নয়:

  • xlLegendPositionCorner
  • xlLegendPositionCustom

আপনার রূপান্তরিত কোডে আংশিকভাবে সমর্থিত API গুলির অসমর্থিত মানগুলিকে ফ্ল্যাগ করার জন্য, library.gs ফাইলে একটি বৈধকরণ শর্ত যোগ করা হয় যা সেই মানগুলি পরীক্ষা করে। উদাহরণস্বরূপ:

if (position == xlLegendPositionCorner ||
     position == xlLegendPositionCustom) {
   position = _handle_legend_position_error(position);
}

যদি যাচাইকারী শর্তটি অসমর্থিত মানগুলির মধ্যে একটি খুঁজে পায়, তাহলে unimplemented_constructs.gs ফাইলে একটি ত্রুটি হ্যান্ডলার ফাংশন, _handle_<API_name>_error , তৈরি করা হয়।

ফাংশনটি একটি ব্যবহারকারীর ত্রুটি ঠেলে দেয় এবং মানটিকে একটি সমর্থিত মান দিয়ে প্রতিস্থাপন করে না। উদাহরণস্বরূপ:

/**
* Throw error message for unsupported legend position.
* The VBA API Legend.Position which can take values xlLegendPositionTop,
* xlLegendPositionLeft, xlLegendPositionBottom, xlLegendPositionRight,
* xlLegendPositionCorner, xlLegendPositionCustom. It is partially supported in
* Apps Scripts that supports only a subset of the values (does not support
* xlLegendPositionCorner and xlLegendPositionCustom).
* @param {string} position
*/
function _handle_legend_position_error(position) {
// Please comment the throw statement and return a supported position value
// instead.
// Values that are supported here are xlLegendPositionTop,
// xlLegendPositionLeft, xlLegendPositionBottom, xlLegendPositionRight.
throw new Error(
   'Google Sheets does not support legend position: ' + position);
}

আংশিকভাবে সমর্থিত API ত্রুটিগুলি ঠিক করুন

আপনার প্রয়োজন অনুসারে একটি গ্রহণযোগ্য সমাধান দিয়ে অসমর্থিত মানগুলি প্রতিস্থাপন করতে _handle_<API_name>_error ফাংশনটি সংজ্ঞায়িত করুন।

  1. ত্রুটির স্থানে রূপান্তরিত অ্যাপস স্ক্রিপ্ট কোডটি খুলুন। ত্রুটি খুঁজুন দেখুন।
  2. কোন মানগুলি সমর্থিত এবং কোনগুলি নয় তা বুঝতে ফাংশনের উপরে মন্তব্যটি পড়ুন।
  3. অসমর্থিত মানগুলির জন্য, কোন সমর্থিত মানগুলি উপযুক্ত প্রতিস্থাপন হিসেবে কাজ করতে পারে তা নির্ধারণ করুন।
  4. _handle_<API_name>_error ফাংশনটি আপডেট করে একটি সমর্থিত মান প্রদান করুন।
  5. যদি আপনি অসমর্থিত মানটি প্রতিস্থাপন করার কোনও উপায় খুঁজে না পান, তাহলে আপনি এই ম্যাক্রোটি রূপান্তর করতে পারবেন না।

আংশিকভাবে সমর্থিত API ত্রুটির উদাহরণ

নিম্নলিখিত উদাহরণটি উপরে উল্লিখিত VBA API legend_position উপর প্রসারিত হয়। আংশিকভাবে সমর্থিত API দেখুন।

নিচে আসল VBA কোডের একটি উদাহরণ দেওয়া হল যা একটি অসমর্থিত মান, xlLegendPositionCustom ব্যবহার করে।

Charts(1).Legend.Position = xlLegendPositionCustom

ম্যাক্রো কনভার্টারটি unimplemented_constructs.gs ফাইলে নিচের ফাংশনটি যোগ করে:

/**
* Throw error message for unsupported legend position.
* The VBA API Legend.Position which can take values xlLegendPositionTop,
* xlLegendPositionLeft, xlLegendPositionBottom, xlLegendPositionRight,
* xlLegendPositionCorner, xlLegendPositionCustom. It is partially supported in
* Apps Scripts that supports only a subset of the values (does not support
* xlLegendPositionCorner and xlLegendPositionCustom).
* @param {string} position
*/
function _handle_legend_position_error(position) {
// Please comment the throw statement and return a supported position value
// instead.
// Values that are supported here are xlLegendPositionTop,
// xlLegendPositionLeft, xlLegendPositionBottom, xlLegendPositionRight.
throw new Error(
   'Google Sheets does not support legend position: ' + position);
}

কায়িক পরিশ্রম প্রয়োজন

ম্যানুয়াল কাজের প্রয়োজন হলে VBA API কে অ্যাপস স্ক্রিপ্টে রূপান্তর করা যেতে পারে, কিন্তু এর জন্য একটি সমাধানের প্রয়োজন।

রূপান্তরের আগে আপনার তৈরি করা সামঞ্জস্য প্রতিবেদনে, এই ধরণের API কে Supported with workarounds হিসাবে লেবেল করা হয়েছে।

যদি আপনি আপনার ফাইল রূপান্তর করার আগে আপনার VBA কোডে এই ধরণের API ঠিক না করেন, তাহলে Apps Script প্রকল্পে এটি কীভাবে প্রদর্শিত হবে তা এখানে দেখুন:

/**
* Could not convert  API. Please add relevant code in the following
* function to implement it.
* This API has been used at the following locations in the VBA script.
*      : 
*
* You can use the following Apps Script APIs to convert it.
* Apps Script APIs : 
* Apps Script documentation links : 
*
* @param param1 {}
* @param param2 {}
* ...
* @return {}
*/
function _api_<API_name>(param1, param2, ....) {
 ThrowException("API  not supported yet.");
}

ম্যানুয়াল কাজের প্রয়োজনীয় ত্রুটিগুলি ঠিক করুন

API যাতে ইচ্ছামতো কাজ করে তার জন্য একটি সমাধান তৈরি করুন। ১. ত্রুটির স্থানে রূপান্তরিত অ্যাপস স্ক্রিপ্ট কোডটি খুলুন। ত্রুটি খুঁজুন দেখুন। ১. কোন API গুলি সমাধানের জন্য ব্যবহার করা যেতে পারে তা বুঝতে ফাংশনের উপরে মন্তব্যটি পড়ুন। ১. যদি আপনি একটি উপযুক্ত সমাধান খুঁজে না পান, তাহলে আপনার কোড থেকে APIটি সরিয়ে ফেলার কথা বিবেচনা করুন। ১. যদি আপনি একটি সমাধান খুঁজে না পান বা আপনার কোড থেকে এই APIটি সরিয়ে ফেলেন এবং আপনার ম্যাক্রো একটি ত্রুটি দেয়, তাহলে আপনি এই ম্যাক্রোটিকে রূপান্তর করতে পারবেন না।

ম্যানুয়ালি কাজের প্রয়োজনীয় ত্রুটির উদাহরণ

এখানে এমন API গুলির উদাহরণ দেওয়া হল যা ম্যানুয়াল কাজের প্রয়োজনীয় ত্রুটিগুলি ফেলে এবং সেগুলি কীভাবে ঠিক করবেন:

উদাহরণ ১: Autocorrect.Addreplacement

নিম্নলিখিত উদাহরণে, VBA API Autocorrect.Addreplacement রূপান্তর করা যেতে পারে, তবে এর জন্য একটি সমাধান প্রয়োজন। ম্যাক্রো কনভার্টার কোড মন্তব্যে ফাংশনটি কীভাবে বাস্তবায়ন করতে হয় তা পরামর্শ দেয়।

/**
* Could not convert autocorrect.addreplacement API. Please add relevant code in
* the following function to implement it.
* This API has been used at the following locations in the VBA script.
*     sheet1 : line 3
* You can use the following Apps Script APIs to convert it.
* Apps Script APIs : FindReplaceRequest , onEdit
* Apps Script documentation links :
* https://developers.google.com/apps-script/reference/script/spreadsheet-trigger-builder#onedit
* https://developers.google.com/sheets/api/eap/reference/rest/v4/spreadsheets/request?hl=en#findreplacerequest

* Comments : AutoCorrect.AddReplacement was not converted, but there is an
* equivalent option you can implement manually. Use onEdit and FindReplaceRequest
* APIs instead, see https://developers.google.com/apps-script/reference/script/spreadsheet-trigger-builder#onedit
* and https://developers.google.com/sheets/api/eap/reference/rest/v4/spreadsheets/request?hl=en#findreplacerequest.
* For more information on API manual implementation, see
* https://developers.google.com/apps-script/guides/macro-converter/fix-conversion-errors.

* @param {Object} CallingObject represents the parent object using which the API
* has been called.
* @param {string} What
* @param {string} Replacement
* @return {string}
*/

function _api_autocorrect_addreplacement(CallingObject, What, Replacement) {
  ThrowException('API autocorrect.addreplacement not supported yet.');

}

Autocorrect.Addreplacement API এর বাস্তবায়ন নিচে দেখানো হল:

var AUTO_CORRECTIONS = "AUTO_CORRECTIONS";
// Need to get the autocorrections set in previous sessions and use them.
var savedAutoCorrections = PropertiesService.getDocumentProperties().getProperty(AUTO_CORRECTIONS);
var autoCorrections = savedAutoCorrections ? JSON.parse(savedAutoCorrections) : {};
function onEdit(e) {
autoCorrect(e.range);
}
function autoCorrect(range) {
for (key in autoCorrections) {
// Replace each word that needs to be auto-corrected with their replacements.
range.createTextFinder(key)
.matchCase(true)
.matchEntireCell(false)
.matchFormulaText(false)
.useRegularExpression(false)
.replaceAllWith(autoCorrections[key]);
}
}
/**
* Could not convert autocorrect.addreplacement API. Please add relevant code in
* the following function to implement it.
* This API has been used at the following locations in the VBA script.
* sheet1 : line 3
*
* You can use the following Apps Script APIs to convert it.
* Apps Script APIs : createTextFinder , onEdit
* Apps Script documentation links : https://developers.google.com/apps-script/reference/script/spreadsheet-trigger-builder#onedit ,
createTextFinder
* Comments : AutoCorrect.AddReplacement was not converted, but there is an
* equivalent option you can implement manually. Use onEdit and FindReplaceRequest
* APIs instead, see https://developers.google.com/apps-script/reference/script/spreadsheet-trigger-builder#onedit
* and createTextFinder. For more information on API manual implementation, see
* https://developers.google.com/apps-script/guides/macro-converter/fix-conversion-errors.
*
* @param {Object} CallingObject represents the parent object using which the API has been called.
* @param {string} What
* @param {string} Replacement
*
* @return {string}
*/

function _api_autocorrect_addreplacement(CallingObject, What, Replacement) {
autoCorrections[What] = Replacement;
// Store the updated autoCorrections in the properties so that future executions use the correction.
PropertiesService.getDocumentProperties().setProperty(AUTO_CORRECTIONS, JSON.stringify(autoCorrections));
}

উদাহরণ ২: Workbook.open()

VBA API workbook.open() একটি ফাইল পাথের উপর ভিত্তি করে একটি স্থানীয় ফাইল খোলে।

ধরুন VBA কোডে workbook.open() দ্বারা দুটি ফাইল খোলা হচ্ছে:

  • ফাইল ১: C:\Data\abc.xlsx
  • ফাইল ২: C:\Data\xyz.xlsx

নিচে দেখানো হল কিভাবে ম্যাক্রো কনভার্টার Workbook.open() কে Apps Script দিয়ে প্রতিস্থাপন করে, যেখানে Workbook.open() ফাইল ১ খোলার জন্য ব্যবহৃত হয়:

var spreadSheetId =
   _handle_mso_excel_get_google_spreadsheet_id("C:\Data\abc.xlsx");
var spreadSheet = SpreadsheetApp.openById(spreadSheetId);
অ্যাপস স্ক্রিপ্ট প্রজেক্টের unimplemented_constructs.gs ফাইলে নিচের ত্রুটিটি যোগ করা হয়েছে:
/**
* Method to return the spreadsheet id manually.
*
* @param {string} FileName ID of the spreadsheet to be opened.
* @return {string} return the spreadsheet id.
*/
function _handle_mso_excel_get_google_spreadsheet_id(FileName) {
 // Upload the Excel files being opened by the API to Google Drive and convert
 // them to Google Sheets.
 // Determine the spreadsheet ID of the Google Sheets file created.
 // Implement this method to return the corresponding spreadsheet ID when given
 //the original file path as parameter.
 throw new Error('Please return the spreadsheet ID corresponding to filename: ' + FileName);
 return '';
}

উপরের নমুনার মন্তব্য অনুসারে, আপনাকে লক্ষ্য ফাইলগুলিকে Google ড্রাইভে Google Sheets ফাইলে রূপান্তর করতে হবে।

সংশ্লিষ্ট গুগল স্প্রেডশিট আইডিগুলি নীচে বোল্ড করা হয়েছে:

  • ফাইল #১: C:\Data\abc.xlsx https://docs.google.com/spreadsheets/d/ abc123Abc123Abc123abc হয়ে যায়
  • ফাইল #২: C:\Data\abc.xlsx https://docs.google.com/spreadsheets/d/ xyz456Xyz456xYz456xyZ হয়ে যায়

তারপর, নীচে দেখানো আইডি অনুসারে ফাইলগুলি খুলতে অ্যাপস স্ক্রিপ্ট ফাংশনে কোডটি পরিবর্তন করুন:

/**
* Method to return the spreadsheet id manually.
*
* @param {string} FileName ID of the spreadsheet to be opened.
* @return {string} return the spreadsheet id.
*/
function _handle_mso_excel_get_google_spreadsheet_id(FileName) {
 // Upload the Excel files being opened by the API to Google Drive and convert
 //them to Google Sheets.
 // Determine the spreadsheet ID of the Google Sheets file created.
 // Implement this method to return the corresponding spreadsheet ID when given
 //the original file path as parameter
 if (Filename.indexOf("abc.xlsx") >= 0) {
   return "abc123Abc123Abc123abc";
 } else if (Filename.indexOf("xyz.xlsx") >= 0) {
   return "xyz456Xyz456xYz456xyZ";
 }

ইচ্ছাকৃত ভুল

আপনার মূল VBA কোডের ত্রুটি আচরণ অনুকরণ করার জন্য আপনার রূপান্তরিত কোডে ইচ্ছাকৃত ত্রুটি যোগ করা হয়। আপনাকে এই ত্রুটিগুলি সংশোধন করার প্রয়োজন নেই।

ইচ্ছাকৃত ভুলের উদাহরণ

VBA তে যদি আপনি একটি অ্যারের সীমানার বাইরে কোনও উপাদান অ্যাক্সেস করার চেষ্টা করেন, তাহলে কোডটি একটি ব্যতিক্রম প্রদান করে। অ্যাপস স্ক্রিপ্টে, কোডটি অনির্ধারিত ফেরত দেয়।

অপ্রত্যাশিত ফলাফল এড়াতে, ম্যাক্রো কনভার্টার অ্যাপস স্ক্রিপ্ট কোড যোগ করে যা একটি অ্যারের সীমার বাইরের উপাদানগুলিতে অ্যাক্সেস করার চেষ্টা করলে একটি ব্যতিক্রম ছুড়ে দেয়।

এই উদাহরণটি নীচের কোডে দেখানো হয়েছে:

মূল VBA কোড
Dim arr
arr = Array("apple", "orange")
MsgBox arr(5)
Will throw the following error:
Subscript out of range
রূপান্তরিত অ্যাপস স্ক্রিপ্ট কোড (ব্যতিক্রম ত্রুটি যোগ করার আগে)
var arr;
arr = ["apple", "orange"];
Browser.msgBox(arr[5]);
Will return this value and not throw an error:
undefined
ব্যতিক্রম ত্রুটি দূর করার জন্য অ্যাপস স্ক্রিপ্ট কোড যোগ করা হয়েছে
/**
* Extend the regular JS array to support VB style indexing with a get method.
* @returns{*} value at the index
*/
Array.prototype.get = function() {
 var curr_res = this;
 for (var i = 0; i < arguments.length; i++) {
   if (!Array.isArray(curr_res) || curr_res.length < arguments[i]) {
     throw new Error(Converted VBA Error (Intentional Error): Subscript out of range);
   }
   curr_res = curr_res[arguments[i]];
 }
 return curr_res;
};
var arr;
arr  = ["apple", "orange"];
Browser.msgBox(arr.get(5));