【问题标题】:Translating Excel Macro to Google Apps Script将 Excel 宏转换为 Google Apps 脚本
【发布时间】:2019-10-14 14:00:35
【问题描述】:

我在 Excel 工作表中使用了一个宏来为特殊计算器制作自定义舍入函数。我想将计算器表上传到我的谷歌驱动器,以便我可以将其作为资源共享。但是,excel 语言无法转换为 Google Apps 脚本语言,我不知道如何调整它。

我不是代码专家,因为我从 excel 中复制了基本代码并稍作调整,可能很幸运能够使其正常工作。但我试图将代码翻译成在谷歌应用程序脚本中工作,但从来没有让它在没有错误的情况下运行。

这是给我一个成功函数的excel代码。

Function pokeRound(pValue As Double) As Double

Dim LWhole As Long
Dim LFraction As Double

'Retrieve integer part of the number
LWhole = Int(pValue)

'Retrieve the fraction part of the number
LFraction = pValue - LWhole

If LFraction <= 0.5 Then
  pokeRound = LWhole
Else
  pokeRound = Round(pValue, 0)
End If

End Function

这是我的翻译尝试失败了

function pokeRound() {
// Retrieve integer part of the number
var LWhole = (pValue);

// Retrieve the fraction part of the number
var LFraction = pValue - LWhole;
 If (LFraction <= 0.5)
   pokeRound = LWhole;
 Else
 (pokeRound = Round(pValue, 0));
};

这个函数的结果是一个自定义的舍入方法。正常四舍五入意味着 88.5 舍入到 89。此函数将 88.5 舍入到 88 和 88.51 到 89 舍入。谁能帮我翻译它以在 Google Apps 脚本中工作?

【问题讨论】:

标签: excel vba google-apps-script google-sheets google-sheets-macros


【解决方案1】:

这个怎么样?

function pokeRound(v) {
  var v=v||1.63;
  var w=Math.floor(v);
  var f=v-w;
  if(f<=0.5) {
    return w;
  }else{
   return Math.round(v);
  }
}

【讨论】:

  • 感谢您的尝试。它给了我我需要弄清楚的东西。顺便说一句,您可以删除var v=v||1.63; 行。我只是用它来测试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多