अपने बदले गए कोड की गड़बड़ियां ठीक करना

मैक्रो कन्वर्टर ऐड-ऑन, कन्वर्ज़न की ज़्यादातर प्रोसेस को अपने-आप करता है. हालांकि, अपने कोड को पूरा करने के लिए, आपको कुछ एपीआई और अन्य आइटम में बदलाव करने पड़ सकते हैं.

अपने प्रोजेक्ट में जोड़ी गई Apps Script फ़ाइलों (GS फ़ाइलें) को समझने, अलग-अलग तरह की गड़बड़ियों को समझने, और गड़बड़ियों को ठीक करने का तरीका जानने के लिए, इस गाइड का इस्तेमाल करें.

अपने प्रोजेक्ट में जोड़ी गई Apps Script फ़ाइलों के बारे में जानना

इन कामों में मदद करने के लिए, आपके Apps Script प्रोजेक्ट में कुछ और GS फ़ाइलें जोड़ी जाती हैं:

  • VBA स्थिरांक और ऐसी वैल्यू तय करें, जो Apps Script में मौजूद नहीं हैं.
  • ऐसे एपीआई लागू करें जिन्हें ग्राहक में नहीं बदला गया है.
  • वैरिएंट रिज़ॉल्व करें.

नीचे दी गई GS फ़ाइलें, आपके Apps Script प्रोजेक्ट में जोड़ी गई हैं:

  • 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 फ़ाइल ऐसे कंस्ट्रक्ट या एपीआई का पता लगाती है जिन्हें मैक्रो कन्वर्टर की मदद से बदला नहीं जा सका. आपको इस फ़ाइल में बदलाव करना पड़ सकता है, ताकि आपका कोड सही तरीके से काम करे.

उदाहरण: Window.Activate()

नीचे, Window.Activate() नाम के एपीआई का उदाहरण दिया गया है जो काम नहीं करता. मैक्रो कन्वर्टर, मिलते-जुलते नाम का एक नया Apps Script फ़ंक्शन बनाता है. साथ ही, उसकी जानकारी unimplemented_constructs.gs फ़ाइल में देता है. VBA फ़ंक्शन काम नहीं करता है, इसलिए नए Apps Script फ़ंक्शन में एक अपवाद दिया जाता है.

नए फ़ंक्शन को बदले गए Apps Script कोड में हर उस जगह जोड़ दिया जाता है जहां ओरिजनल एपीआई का इस्तेमाल VBA कोड में किया जाता था.

अगर आपको ओरिजनल एपीआई का व्यवहार फिर से बनाने का कोई तरीका मिलता है, तो आपको सिर्फ़ unimplemented_constructs.gs फ़ाइल में फ़ंक्शन की परिभाषा को अपडेट करना होगा. फ़ंक्शन के वहां बताने के बाद, यह आपके Apps Script प्रोजेक्ट में हर उस जगह लागू होता है जहां फ़ंक्शन दिखता है.

यहां कोड में यह उदाहरण दिया गया है:

मूल VBA कोड

Window.activate()

बदला हुआ Apps Script कोड, इन-लाइन जोड़ा गया

_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 फ़ाइल को आपके Apps Script प्रोजेक्ट में जोड़ दिया जाता है. ऐसा कई वजहों से हो सकता है. जैसे, ऐसा एपीआई जिसमें कई रिटर्न टाइप हों या ऑब्जेक्ट को खुद एक वैरिएंट के तौर पर बताया गया हो.

मैक्रो कन्वर्टर, इस फ़ाइल में __handle_resolve_<api>() नाम का एक नया फ़ंक्शन जोड़ता है. यह फ़ंक्शन, बताए गए एपीआई को बदल देता है और ऑब्जेक्ट का टाइप तय करने में मदद करता है.

कुछ मामलों में, आपको ऑब्जेक्ट टाइप को मैन्युअल तरीके से बताने के लिए, __handle_resolve_<api>() फ़ंक्शन को अपडेट करना पड़ सकता है. काम न करने वाले ऑब्जेक्ट प्रकार देखें.

उदाहरण: name()

VBA में कई ऑब्जेक्ट टाइप, name() API को परिभाषित करते हैं. आम तौर पर, Apps स्क्रिप्ट getName() के बराबर होती है, लेकिन हर ऑब्जेक्ट टाइप के लिए नहीं. इसके कई विकल्प हो सकते हैं:

  • ऑब्जेक्ट के मिलते-जुलते एपीआई को getName() से अलग कुछ कहा जाता है.
  • ऑब्जेक्ट का नाम पाने के लिए, Apps Script API मौजूद नहीं है.
  • कोई मिलता-जुलता Apps Script ऑब्जेक्ट नहीं है.

ऑब्जेक्ट टाइप की पहचान न होने पर, मैक्रो कन्वर्टर, variant_resolutions.gs फ़ाइल में __handle_resolve_name नाम का एक नया फ़ंक्शन बनाता है.

यहां कोड में यह उदाहरण दिया गया है:

मूल VBA कोड

a = Selection.name

इस मामले में, मौजूदा चुने गए फ़ंक्शन पर एपीआई name() को कॉल किया जाता है. चुना गया हिस्सा, शीट ऑब्जेक्ट या आकार ऑब्जेक्ट हो सकता है. अगर यह कोई शीट ऑब्जेक्ट है, तो अनुवाद getName() है, लेकिन अगर यह आकार ऑब्जेक्ट है, तो Apps Script में इसके बराबर कोई चीज़ नहीं है.

बदला हुआ 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;
}

गड़बड़ियां खोजें

बदले गए Apps Script कोड में कोई गड़बड़ी होने पर, मैसेज गड़बड़ी का टाइप और उसकी जगह के बारे में बताता है. गड़बड़ी के मैसेज का फ़ॉर्मैट, इस बात पर निर्भर करता है कि किस Apps Script रनटाइम का इस्तेमाल किया जा रहा है.

अगर आप डिफ़ॉल्ट V8 रनटाइम का इस्तेमाल कर रहे हैं, तो आपको एक गड़बड़ी दिखेगी. यह गड़बड़ी कुछ ऐसी दिखेगी:

_api_windows_active (unimplemented_constructs:2:3)

इसका मतलब है कि गड़बड़ी, unimplemented_constructs.gs फ़ाइल की लाइन 2 के वर्ण 3 में मौजूद है.

अगर आप अब काम न करने वाले Rhino रनटाइम पर हैं, तो आपको एक गड़बड़ी दिखेगी. यह गड़बड़ी कुछ ऐसी दिखेगी:

unimplemented_constructs:2 (_api_windows_active)

इसका मतलब है कि गड़बड़ी unimplemented_constructs.gs फ़ाइल में दूसरी लाइन में मौजूद है.

गड़बड़ी के टाइप

ऊपर बताई गई unimplemented_constructs.gs और variant_resolution.gs फ़ाइलों में होने वाली ज़्यादातर गड़बड़ियों को ठीक किया जा सकता है.

आपको मिलने वाली गड़बड़ियों के टाइप में ये शामिल हैं:

लागू नहीं किया गया एपीआई

लागू नहीं किया गया एपीआई एक ऐसा एपीआई है जिसे मैक्रो कन्वर्टर, VBA से Apps Script में नहीं बदल सकता. साथ ही, इस एपीआई के लिए कोई कारगर तरीका उपलब्ध नहीं है.

लागू नहीं किए गए एपीआई को आम तौर पर खाली फ़ंक्शन के रूप में—कभी-कभी खाली हस्ताक्षरों के साथ—unimplemented_constructs.gs फ़ाइल में जोड़ा जाता है. अगर ऑब्जेक्ट टाइप तय नहीं हो पाता है, तो लागू नहीं किए गए एपीआई को variant_resolution.gs फ़ाइल में जोड़ा जा सकता है.

कन्वर्ज़न से पहले जनरेट की गई कम्पैटबिलटी रिपोर्ट में, इस एपीआई को ज़्यादा जांच की ज़रूरत है के तौर पर लेबल किया जाता है.

अगर आपने अपनी फ़ाइल में बदलाव करने से पहले अपने 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_(param1, param2, ....) {
  ThrowException("API  not supported yet.");
}

लागू नहीं किए गए एपीआई की गड़बड़ियां ठीक करना

मौजूदा Apps Script API या JS लाइब्रेरी की मदद से, लागू नहीं किए गए एपीआई के बारे में बताएं. ऐसा करने के लिए, यह तरीका अपनाएं:

  1. गड़बड़ी वाली जगह पर, बदला गया Apps Script कोड खोलें. गड़बड़ियां ढूंढें देखें.
  2. फ़ंक्शन के ऊपर, जोड़ी गई टिप्पणी को पढ़ें. कुछ मामलों में, टिप्पणी, Apps Script में एपीआई लागू करने के तरीके का सुझाव देती है.
  3. अगर आपको Apps Script में, एपीआई लागू करने का कोई तरीका नहीं मिलता है, तो इसे अपने कोड से हटाएं.
  4. अगर आपको कोई समाधान नहीं मिलता या अपने कोड से यह एपीआई हटा दिया जाता है और आपका मैक्रो यह गड़बड़ी दिखाता है, तो इस मैक्रो को बदला नहीं जा सकता.

लागू नहीं किए गए एपीआई की गड़बड़ियों के उदाहरण

यहां एपीआई के लागू न होने की स्थितियों और उन्हें ठीक करने के उदाहरण दिए गए हैं:

  • इससे मिलती-जुलती कोई Apps स्क्रिप्ट नहीं है: यह एक ऐसा एपीआई है जो Chart.Protect के लिए सीधे तौर पर काम नहीं करता है, जो Apps Script में मौजूद नहीं है.
  • अनजान ऑब्जेक्ट टाइप: इसमें, वैरिएबल वाले ऑब्जेक्ट टाइप को मैनेज करने का तरीका बताया गया है. साथ ही, इसमें काम न करने वाले ऐसे ऑब्जेक्ट टाइप को लागू करने का तरीका बताया गया है जिसे Apps Script में फिर से बनाया जा सकता है.
उदाहरण 1: कोई मिलती-जुलती Apps स्क्रिप्ट या अज्ञात एपीआई नहीं है

इस उदाहरण में, 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();
}
}
दूसरा उदाहरण: इस टाइप के ऑब्जेक्ट का इस्तेमाल नहीं किया जा सकता

अगर ऑब्जेक्ट टाइप की जानकारी नहीं है, तो variant_resolution.gs फ़ाइल में एपीआई की ऐसी गड़बड़ी जोड़ दी जाती है जो लागू नहीं होती. इस उदाहरण में, ऊपर दिए गए VBA name() API के उदाहरण के बारे में बताया गया है. variant_resolution.gs देखें.

इस उदाहरण में आपको इनके बारे में जानकारी मिलेगी:

  1. variant_resolution.gs फ़ाइल में, name() एपीआई को नए फ़ंक्शन में कैसे बदला जाता है.
  2. बदले गए कोड में नए फ़ंक्शन को कॉल करने का तरीका.
  3. Apps Script में CommandBar के लिए, काम न करने वाला ऑब्जेक्ट टाइप कैसे बनाएं.

1. बदला गया कोड, ऑब्जेक्ट का वह टाइप तय नहीं कर पा रहा है जिस पर 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;
}

2. मान लीजिए कि 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));
}

3. मान लें कि आपका VBA कोड, ऑब्जेक्ट टाइप CommandBar पर PrintName() फ़ंक्शन को कॉल करता है. VBA कोड नीचे दिखाया गया है:

PrintName Application.CommandBars.item("Standard")
CommandBar, Apps Script में काम नहीं करता. इस वजह से, ऊपर दिए गए VBA कोड में इस्तेमाल किए गए दो तरीके भी काम नहीं करते.
  • Application.CommandBars(): VBA में, यह सभी CommandBar ऑब्जेक्ट की सूची दिखाता है.
  • CommandBars.item(): VBA में, यह एक खास CommandBar ऑब्जेक्ट दिखाता है.
इस ऑब्जेक्ट टाइप को Apps Script में इस्तेमाल नहीं किया जा सकता. इसलिए, नए कोड के तौर पर बदला गया कोड, `un Implemented_structs.gs` फ़ाइल में ये फ़ंक्शन बनाता है, जिन्हें आपको तय करना होता है.
  • _api_application_commandbars()
  • _api_commandbars_item()
बदले गए कोड में फ़ंक्शन कॉल किए जाते हैं, जैसा कि यहां दिखाया गया है:
PrintName(_api_commandbars_item(_api_application_commandbars(), "Standard")))

Here’s 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.');
}

नए फ़ंक्शन ठीक से काम करें, इसके लिए यह तरीका अपनाएं:

3.1 एक नया ऑब्जेक्ट टाइप तय करें जो VBA की तरह ही CommandBars की फ़ंक्शन और CommandBars का नया कलेक्शन बनाता हो.

3.2 नए ऑब्जेक्ट टाइप के लिए, getName() तरीका जोड़ें.

नीचे दिए गए कोड में, चरण 3.1 और 3.2 दिखाए गए हैं. मेन्यू ऑब्जेक्ट को एक नए ऑब्जेक्ट टाइप के तौर पर बनाया जाता है, जो 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();

3.3 नए ऑब्जेक्ट टाइप को हैंडल करने के लिए, 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;
}

3.4 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);
}

लागू नहीं किए गए लैंग्वेज कंस्ट्रक्ट

construct कोड लैंग्वेज का एक एलिमेंट है, जो एक्ज़ीक्यूशन फ़्लो या डेटा डिसप्ले को कंट्रोल करता है. उदाहरण के लिए, लूप, लेबल, इवेंट, और गोटो. यहां सभी VBA कंस्ट्रक्ट की सूची दी गई है.

ऐसे सिस्टम जिनमें मैक्रो कन्वर्टर को कन्वर्ट नहीं किया जा सकता, उन्हें लागू नहीं किया गया लैंग्वेज कंस्ट्रक्शन माना जाता है.

जहां मैक्रो कन्वर्टर यह तय करता है कि लागू न किया गया कोई लैंग्वेज कंस्ट्रक्ट मौजूद है, तो वह TODO टिप्पणी डाल देता है.

नीचे दिए गए VBA कंस्ट्रक्शन का इस्तेमाल नहीं किया जा सकता:

लागू नहीं की गई भाषा बनाने से जुड़ी गड़बड़ियां ठीक करना

  1. अपना कोड अपडेट करें, ताकि आपका लॉजिक, इस्तेमाल न की जा सकने वाली भाषा पर निर्भर न हो.
  2. गड़बड़ी वाली जगह पर, बदला गया Apps Script कोड खोलें. गड़बड़ियां ढूंढें देखें.
  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
समान Apps स्क्रिप्ट कोड
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);
}

उदाहरण 2: GoTo को लूप के लिए बदलें

मूल 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
समान Apps स्क्रिप्ट कोड
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);
}

आंशिक रूप से काम करने वाला एपीआई

आंशिक रूप से काम करने वाले एपीआई के लिए, कुछ इनपुट पैरामीटर Apps Script में काम करते हैं और कुछ नहीं.

उदाहरण के लिए, VBA API legend_position का इस्तेमाल Excel ग्राफ़ में लेजेंड बनाने के लिए किया जाता है. यह कई तरह की इनपुट वैल्यू के साथ काम करता है. इनमें ये शामिल हैं:

  • xlLegendPositionBottom: लेजेंड को चार्ट के निचले हिस्से में डालता है.
  • xlLegendPositionCorner: लेजेंड को चार्ट के कोने में रखता है.
  • xlLegendPositionCustom: लेजेंड को चार्ट में अपनी पसंद की जगहों पर रखता है.

Apps Script में एक जैसा कोड है. यह कोड, इनमें से सिर्फ़ कुछ वैल्यू के साथ काम करता है. नीचे दिए गए मान काम नहीं करते:

  • xlLegendPositionCorner
  • xlLegendPositionCustom

आपके बदले गए कोड में, कुछ हद तक काम करने वाले एपीआई की काम न करने वाली वैल्यू को फ़्लैग करने के लिए, 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);
}

कुछ हद तक काम करने वाले एपीआई की गड़बड़ियों को ठीक करना

काम न करने वाली वैल्यू को अपनी ज़रूरतों के हिसाब से सही समाधान से बदलने के लिए, _handle_<API_name>_error फ़ंक्शन तय करें.

  1. गड़बड़ी वाली जगह पर, बदला गया Apps Script कोड खोलें. गड़बड़ियां ढूंढें देखें.
  2. कौनसी वैल्यू इस्तेमाल की जा सकती हैं और कौनसी नहीं, यह समझने के लिए फ़ंक्शन के ऊपर दी गई टिप्पणी पढ़ें.
  3. यह तय करें कि इस्तेमाल न की जा सकने वाली कौनसी वैल्यू, आपके हिसाब से सही वैल्यू के तौर पर काम कर सकती हैं.
  4. इसके बजाय, इस्तेमाल की जा सकने वाली वैल्यू दिखाने के लिए _handle_<API_name>_error फ़ंक्शन को अपडेट करें.
  5. अगर आपको काम न करने वाली वैल्यू को बदलने का तरीका नहीं मिलता, तो इस मैक्रो को कन्वर्ट नहीं किया जा सकता.

कुछ हद तक काम करने वाले एपीआई की गड़बड़ी का उदाहरण

नीचे दिया गया उदाहरण, ऊपर बताए गए VBA API legend_position के बारे में विस्तार से बताता है. कुछ हद तक काम करने वाला एपीआई देखें.

नीचे ओरिजनल 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 को Apps Script में बदला जा सकता है, लेकिन इसे ठीक करने की ज़रूरत है.

कन्वर्ज़न से पहले जनरेट की गई कम्पैटबिलटी रिपोर्ट में, इस तरह के एपीआई को समस्याओं को हल करने के तरीके के तौर पर लेबल किया जाता है.

अगर आपने अपनी फ़ाइल में बदलाव करने से पहले अपने 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_(param1, param2, ....) {
 ThrowException("API  not supported yet.");
}

मैन्युअल तरीके से किए जाने वाले काम से जुड़ी गड़बड़ियों को ठीक करना

एपीआई के लिए कोई समाधान लागू करें, ताकि एपीआई सही तरीके से काम करे. 1. गड़बड़ी वाली जगह पर, बदला गया Apps Script कोड खोलें. गड़बड़ियां ढूंढें देखें. 1. यह समझने के लिए कि समाधान के लिए कौनसे एपीआई इस्तेमाल किए जा सकते हैं, फ़ंक्शन के ऊपर दी गई टिप्पणी पढ़ें. 1. अगर आपको सही तरीका नहीं मिलता है, तो अपने कोड से एपीआई को हटा दें. 1. अगर आपको कोई समाधान नहीं मिल रहा है या अपने कोड से यह एपीआई हटा दिया गया है और आपका मैक्रो गड़बड़ी दिखाता है, तो इस मैक्रो को बदला नहीं जा सकता.

मैन्युअल तरीके से काम करने से जुड़ी गड़बड़ियों के उदाहरण

यहां ऐसे एपीआई के उदाहरण दिए गए हैं जिनमें मैन्युअल तरीके से काम करने से जुड़ी गड़बड़ियां होती हैं और उन्हें ठीक करने के तरीके भी बताए गए हैं:

उदाहरण 1: 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));
}

उदाहरण 2: Workbook.open()

VBA API workbook.open(), फ़ाइल पाथ के आधार पर लोकल फ़ाइल खोलता है.

मान लें कि workbook.open() के ज़रिए VBA कोड में दो फ़ाइलें खोली जा रही हैं:

  • फ़ाइल 1: C:\Data\abc.xlsx
  • फ़ाइल 2: C:\Data\xyz.xlsx

नीचे दिखाया गया है कि मैक्रो कन्वर्टर कैसे Workbook.open() को Apps स्क्रिप्ट से बदल देता है जहां फ़ाइल 1 को खोलने के लिए Workbook.open() का इस्तेमाल किया जाता है:

var spreadSheetId =
   _handle_mso_excel_get_google_spreadsheet_id("C:\Data\abc.xlsx");
var spreadSheet = SpreadsheetApp.openById(spreadSheetId);
Apps Script प्रोजेक्ट की 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 Drive पर Google Sheets फ़ाइलों में बदलना होगा.

संबंधित Google स्प्रेडशीट आईडी नीचे बोल्ड किए गए हैं:

  • फ़ाइल #1: C:\Data\abc.xlsx का नाम https://docs.google.com/spreadsheets/d/abc123Abc123Abc123abc हो जाएगा
  • फ़ाइल #2: C:\Data\abc.xlsx का नाम https://docs.google.com/spreadsheets/d/xyz456Xyz456xYz456xyZ हो जाएगा

फिर, फ़ाइलों को आईडी के हिसाब से खोलने के लिए, Apps Script फ़ंक्शन में कोड में बदलाव करें, जैसा कि नीचे दिखाया गया है:

/**
* 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 में किसी अरे की सीमा से बाहर के किसी एलिमेंट को ऐक्सेस करने की कोशिश की जाती है, तो कोड एक अपवाद देता है. Apps Script में, कोड के बारे में कोई जानकारी नहीं दी गई है.

अनचाहे नतीजों से बचने के लिए, मैक्रो कन्वर्टर, Apps स्क्रिप्ट कोड जोड़ता है. इससे, किसी अरे की सीमाओं से बाहर के एलिमेंट को ऐक्सेस करने की कोशिश करने पर, एक अपवाद दिखता है.

यह उदाहरण नीचे दिए गए कोड में दिखाया गया है:

मूल VBA कोड
Dim arr
arr = Array("apple", "orange")
MsgBox arr(5)
Will throw the following error:
Subscript out of range
ग्राहक में बदला गया Apps स्क्रिप्ट कोड (अपवाद की गड़बड़ी जोड़े जाने से पहले)
var arr;
arr = ["apple", "orange"];
Browser.msgBox(arr[5]);
Will return this value and not throw an error:
undefined
अपवाद की गड़बड़ी दिखाने के लिए Apps Script कोड जोड़ा गया
/**
* 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));