Пример #1
0
        public static object OCTOPART_DETAIL_URL(
            [ExcelArgument(Description = "Part Number Lookup", Name = "MPN or SKU")] string mpn_or_sku,
            [ExcelArgument(Description = "Manufacturer of the part (optional)", Name = "Manufacturer")] string manuf = "")
        {
            // Check to see if a cached version is available (only checks non-error'd queries)
            ApiV4Schema.Part part = GetManuf(mpn_or_sku, manuf);
            if (part != null)
                return part.octopart_url;

            // Excel's recalculation engine is based on INPUTS. The main function will be called if:
            // - Inputs are changed
            // - Input cells location are changed (i.e., moving the cell, or deleting a row that impacts this cell)
            // However, the async function will ONLY be run if the inputs are DIFFERENT.
            //   The impact of this is that if last time the function was run it returned an error that was unrelated to the inputs 
            //   (i.e., invalid ApiKey, network was down, etc), then the function would not run again.
            // To fix that issue, whitespace padding is added to the mpn_or_sku. This whitespace is removed anyway, so it has no 
            // real impact other than to generate a refresh.
            mpn_or_sku = mpn_or_sku.PadRight(mpn_or_sku.Length + _refreshhack.Length);
#if !TEST
            object asyncResult = ExcelAsyncUtil.Run("OCTOPART_DETAIL_URL", new object[] { mpn_or_sku, manuf }, delegate
            {
#endif
            try
            {
                part = SearchAndWaitPart(mpn_or_sku, manuf);
                if (part == null)
                {
                    string err = QueryManager.GetLastError(mpn_or_sku);
                    if (string.IsNullOrEmpty(err))
                        err = "Query did not provide a result. Please widen your search criteria.";

                    return "ERROR: " + err;
                }

                return part.octopart_url;
            }
            catch (Exception ex)
            {
                log.Fatal(ex.ToString());
                return "ERROR: " + OctopartQueryManager.FATAL_ERROR;
            }
#if !TEST
            });

            if (asyncResult.Equals(ExcelError.ExcelErrorNA))
            {
                // Still processing...
                return "!!! Processing !!!";
            }
            else if (!string.IsNullOrEmpty(QueryManager.GetLastError(mpn_or_sku)) || (part == null))
            {
                // Regenerate the hack value if an error was received
                _refreshhack = string.Empty.PadRight(new Random().Next(0, 100));
            }
            
            // Done processing!
            return asyncResult;
#endif
        }
Пример #2
0
        /// <summary>
        /// Gets the url of the datasheet; returns first option if available
        /// </summary>
        /// <param name="part">The part as returned by the search</param>
        /// <returns>The url for the 'best' datasheet</returns>
        public static string GetDatasheetUrl(this ApiV4Schema.Part part)
        {
            if ((part != null) && (part.datasheets != null))
            {
                var datasheet = part.datasheets.FirstOrDefault(i => !string.IsNullOrEmpty(i.url));
                if (datasheet != null)
                {
                    // Success!
                    return(datasheet.url);
                }
            }

            return("ERROR: Datasheet url not found. Please try expanding your search");
        }
Пример #3
0
        public static object OCTOPART_DATASHEET_URL(
            [ExcelArgument(Description = "Part Number Lookup", Name = "MPN or SKU")] string mpn_or_sku,
            [ExcelArgument(Description = "Manufacturer of the part to query (optional)", Name = "Manufacturer")] string manuf = "")
        {
            ApiV4Schema.Part part = GetManuf(mpn_or_sku, manuf);
            if (part != null)
            {
                // ---- BEGIN Function Specific Information ----
                return part.GetDatasheetUrl();
                // ---- END Function Specific Information ----
            }

            mpn_or_sku = mpn_or_sku.PadRight(mpn_or_sku.Length + _refreshhack.Length);
            object asyncResult = ExcelAsyncUtil.Run("OCTOPART_DATASHEET_URL", new object[] { mpn_or_sku, manuf }, delegate
            {
                try
                {
                    part = SearchAndWaitPart(mpn_or_sku, manuf);
                    if (part == null)
                    {
                        string err = QueryManager.GetLastError(mpn_or_sku);
                        if (string.IsNullOrEmpty(err))
                            err = "Query did not provide a result. Please widen your search criteria.";

                        return "ERROR: " + err;
                    }

                    // ---- BEGIN Function Specific Information ----
                    return part.GetDatasheetUrl();
                    // ---- END Function Specific Information ----
                }
                catch (Exception ex)
                {
                    log.Fatal(ex.ToString());
                    return "ERROR: " + OctopartQueryManager.FATAL_ERROR;
                }
            });

            if (asyncResult.Equals(ExcelError.ExcelErrorNA))
            {
                return "!!! Processing !!!";
            }
            else if (!string.IsNullOrEmpty(QueryManager.GetLastError(mpn_or_sku)) || (part == null))
            {
                _refreshhack = string.Empty.PadRight(new Random().Next(0, 100));
            }

            return asyncResult;
        }