Google Sheets运行库

huangapple go评论67阅读模式
英文:

Google Sheets Run Library

问题

I got a problems to run a library on google sheet.

this is my library. Actually I'm not sure if it is, I just typed out my code and deploy it as a library only. Is this considered a library?
https://script.google.com/d/1caK6rW1--NmMSzSq0DHT0d4J-ucIdPv7KDPOmo16mZ9LBEZJo1OxXm8K/edit?usp=sharing

/**

  • Finds a combination of numbers that add up to a specific value.
  • @param {A1:A16} data The range of cells containing the data.
  • @param {B1} target The target value.
  • @return An array containing the combination of values that add up to the target value.
  • @customfunction
    */
    function findCombination(data, target) {
    var values = data;
    var n = values.length;
    var found = false;
    var result = [];

for (var i = 1; i < Math.pow(2, n); i++) {
var sum = 0;
for (var j = 0; j < n; j++) {
if ((i & (1 << j)) !== 0) {
sum += values[j][0];
}
}

if (sum === target) {
  result = values.map(function(value, index) {
    if ((i &amp; (1 &lt;&lt; index)) !== 0) {
      return value[0];
    } else {
      return &quot;&quot;;
    }
  });
  found = true;
  break;
}

}

if (!found) {
result = ["No combination found"];
}

return result;
}

And then is this my code on google sheets,

function run(data, target) {

Mylibrary.findCombination(data, target)

}

But when I type the run function in google sheet, it doesn't work very well. No error is being reported either, I don't understand how to improve it.

The purpose of this is to integrate all my scattered code into one library, and then other sheets call the lib for easy maintenance.

英文:

I got a problems to run a library on google sheet.

this is my library. Actually I'm not sure if it is, I just typed out my code and deploy it as a library only. Is this considered a library?
https://script.google.com/d/1caK6rW1--NmMSzSq0DHT0d4J-ucIdPv7KDPOmo16mZ9LBEZJo1OxXm8K/edit?usp=sharing

/**
 * Finds a combination of numbers that add up to a specific value.
 *
 * @param {A1:A16} data The range of cells containing the data.
 * @param {B1} target The target value.
 * @return An array containing the combination of values that add up to the target value.
 * @customfunction
 */
function findCombination(data, target) {
  var values = data;
  var n = values.length;
  var found = false;
  var result = [];
  
  for (var i = 1; i &lt; Math.pow(2, n); i++) {
    var sum = 0;
    for (var j = 0; j &lt; n; j++) {
      if ((i &amp; (1 &lt;&lt; j)) !== 0) {
        sum += values[j][0];
      }
    }
    
    if (sum === target) {
      result = values.map(function(value, index) {
        if ((i &amp; (1 &lt;&lt; index)) !== 0) {
          return value[0];
        } else {
          return &quot;&quot;;
        }
      });
      found = true;
      break;
    }
  }
  
  if (!found) {
    result = [&quot;No combination found&quot;];
  }
  
  return result;
}

And then is this my code on google sheets,

function run(data, target) {

  Mylibrary.findCombination(data, target)

}

But when I type the run function in google sheet, it doesn't work very well. No error is being reported either, I don't understand how to improve it.

The purpose of this is to integrate all my scattered code into one library, and then other sheets call the lib for easy maintenance.

答案1

得分: 0

如果您的Google Apps Script项目正确将您的脚本安装为库,不幸的是,run(data, target) 并没有从Mylibrary.findCombination(data, target) 中返回,因为没有return。我认为这是您当前问题的原因。如果我的理解是正确的,请按照以下方式修改您的显示脚本。

修改后的脚本:

function run(data, target) {
  return Mylibrary.findCombination(data, target);
}

参考

英文:

If your Google Apps Script project including run(data, target) correctly installs your script as a library, unfortunately, run(data, target) is not returned from Mylibrary.findCombination(data, target), because of no return. I think that this is the reason for your current issue. If my understanding is correct, please modify your showing script as follows.

Modified script:

function run(data, target) {
  return Mylibrary.findCombination(data, target);
}

Reference

huangapple
  • 本文由 发表于 2023年6月13日 11:28:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461513.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定