简体   繁体   English

在Google工作表中将JS代码调整为Apps脚本

[英]Adjusting JS code to Apps Script in Google sheet

I have a working code in JS for my Web Application, which generates custom-format report ID number with the pattern NSAA-####-####-YYYY, where AA - any letter, #### any number, YYYY current year. 我在Web应用程序的JS中有一个工作代码,该代码会生成自定义格式的报告ID号,其格式为NSAA-####-####-YYYY,其中AA-任何字母,####任何数字, YYYY当前年份。 This function in JS is quoted below. 下面引用了JS中的此函数。

I was asked to try to transfer the same functionality to Google sheet, as a "custom function." 我被要求尝试将相同的功能作为“自定义功能”转移到Google表格中。

Although most of the code works fine, I get issues with indexOf methods in GAS and overall I am not sure if I am doing it correctly at all. 尽管大多数代码都能正常工作,但GAS中的indexOf方法还是有问题,总体而言,我不确定我是否做得正确。 Would appreciate a quick hint on this. 希望对此有个快速提示。

This is JS code I am trying to reproduce in Apps Script: 这是我尝试在Apps脚本中复制的JS代码:

let d = new Date();
const randomString = (length, chars) => {
  let mask = '';

  if (chars.indexOf('A') > -1) mask += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  if (chars.indexOf('#') > -1) mask += '0123456789';
  let result = '';
  for (let i = length; i > 0; --i) result += mask[Math.round(Math.random() * (mask.length - 1))];
  return result;
}

// Inserting report ID into appropriate fields
const reportNS = () => {
  let reportIDNS = document.getElementById("reportIDNS");
  reportIDNS.value = 'NS' + randomString(2, 'A') + '-' + randomString(4, '#') + '-' + randomString(4, '#') + '-' +
    d.getFullYear();
}

My first idea was to put it in someting like this: 我的第一个想法是将其放入这样的东西中:

/**
 */

var d = new Date();
function randomString (length, chars) {
  var mask = '';

  if (chars.indexOf('A') > -1) mask += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  if (chars.indexOf('#') > -1) mask += '0123456789';
  var result = '';

  for (var i = length; i > 0; --i)
    result += mask[Math.round(Math.random() * (mask.length - 1))];
  return result = 'NS' + randomString(2, 'A') + '-' + randomString(4, '#') + '-' + randomString(4, '#') + '-' + d.getFullYear();
}

However, I get the error 但是,我得到了错误

TypeError: Cannot call method "indexOf" of undefined. TypeError:无法调用未定义的方法“ indexOf”。

Would appreciate a hint on this! 希望对此有所提示!

Got it - here how it works. 知道了-这是如何运作的。 Of course - need to use the second function to call that randomString: 当然-需要使用第二个函数来调用那个randomString:

// Funciton to create ReportID (or caseID) as the customer wants
function CaseID () {
  var d = new Date();
  var caseID = 'ID' + randomString(2, 'A') + '-' + randomString(4, '#') + '-' + randomString(4, '#') + '-' + d.getFullYear();
  return caseID;
} 

// Function that generates randomString - no surprises here
function randomString (length, chars) {
  var mask = '';

     if (chars.indexOf('A') > -1) mask += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
     if (chars.indexOf('#') > -1) mask += '0123456789';
      var result = '';

  for (var i = length; i > 0; --i) {
    result += mask[Math.round(Math.random() * (mask.length - 1))]
  };
      return result;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM