对于循环迭代和replaceText [英] for loop iteration and replaceText

查看:124
本文介绍了对于循环迭代和replaceText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经改变了脚本如下:

I have changed the script to the following:

  function readRows(){
   var nums = ["Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"];

   function toText(num) {
   var s;
   if (num >= 100)
    throw "Too big";
   if (num > 10) {
    if (num < 20) {
        switch (num) {
            case 11:
                return "Eleven";
            case 12:
                return "Twelve";
            case 13:
                return "Thirteen";
            case 14:
                return "fourteen";
            case 15:
                return "Fifteen";
            case 16:
                return "sixteen";
            case 17:
                return "seventeen";
            case 18:
                return "eighteen";
            case 19:
                return "nineteen";
            default:
                return toText(num-10)+"teen";
        }
    }

    switch (Math.floor(num / 10)) {
        case 2:
            s = "Twenty";
            break;
        case 3:
            s = "Thirty";
            break;
        case 4:
            s = "Fourty";
           break;
        case 5:
            s = "Fifty";
            break;
        case 6:
            s= "SiXty";
            break;
        case 7:
            s = "Seventy";
            break;
        default:
            s = toText(Math.floor(num/10))+"ty";
            break;
    }
    if(num > 20 && num < 30)
       {
        return "twenty"+toText(num-20);
       }
    if(num > 30 && num < 40)
       {
        return "thirty"+toText(num-30);
       }
    if(num > 40 && num < 50)
       {
        return "fourty"+toText(num-40);
       }
    if(num > 50 && num < 60)
       {
        return "fifty"+toText(num-50);
       }
    if(num > 60 && num < 70)
       {
        return "sixTy"+toText(num-60);
       }
    if(num == 70)
       {
        return "sEvEnty";
       }
    if (num % 10 > 0)
        return s + toText(num % 10);
    return s;
    }

    return nums[num];
    }


    var sheet = SpreadsheetApp.getActiveSheet();
    var lastCol = sheet.getLastColumn();
    var length = sheet.getMaxColumns();
    var rows = sheet.getMaxRows();

    var Name = sheet.getRange(2, 2).getValue(); 
    var data = sheet.getRange(2, 1, 2, lastCol);  
    var template = "spreadsheet key";
    var fileName = "Application document template.docx";
    var newFile = DocsList.getFileById(template).makeCopy(Name + " Application for Phase1 NF3").getId();
    var doc = DocumentApp.openById(newFile);
    var body = doc.getActiveSection();

    for(var j=1; j<length; j++)
    {
    body.replaceText("Answer"+toText(j), data[0][j]);   // line 105
    }
    }

在一个阶段,我除了用70几乎工作,做了一件再没有什么工作。返工它,现在我得到这个错误,
类型错误:无法从不确定的读取属性1.0。 (行105文件ConvertToDocs)。
谁能告诉我的财产1.0可能指的是什么?这是为什么$ C $的第一线c不是在code窗口,显示?

At one stage I had it almost working except for replacing 70, did something and then nothing worked. Reworked it, now I'm getting this error, TypeError: Cannot read property "1.0" from undefined. (line 105, file "ConvertToDocs"). Can anyone advise me what the property "1.0" might refer to? Why is that first line of code not showing in the code window?

推荐答案

目前还不清楚你的问题是什么 - 你有在标题的提示,但问东西在文本中的不同。让我们看看我们能做些什么。

It's not clear what your question is - you've got hints in the Title, but ask something different in the text. Let's see what we can do.

第一 - 为什么错误?这很简单:数据定义为范围,但你要访问它作为一个二维数组。你可能想 VAR数据= sheet.getRange(...)。的GetValues​​(),它会给你的细胞的的内容的范围。

First - why the error? That's easy: data is defined as a Range, but you're trying to access it as a two-dimensional array. You probably wanted var data = sheet.getRange(...).getValues(), which would give you the content of the cells in the range.

您正在做别的事情怪在那里。您定义的数据范围与 .getRange(2,1,2,lastCol),这是 A2:x3`; 2行,X = maxColumns。后来的后来,你试试遍历只有一个行和'maxColumns - 这是不正确的存在,但只有你知道你想要做什么。

You're doing something else strange there. You define the data range with .getRange(2, 1, 2, lastCol)', which isA2:x3`; 2 rows, x=maxColumns. Then later, you try to iterate over just one row and 'maxColumns' - something isn't right there, but only you know what you wanted to do.

- 你提到循环。你有一个数组迭代错误:你循环这样的 - 为(VAR J = 1; J&LT;长度; J ++)。这样做的问题是阵列0开始,所以你访问时将跳过第一个元素数据[] [J]

Second - you mention looping. You've got an Array Iteration Bug: You're looping like this - for(var j=1; j<length; j++). The problem with that is arrays start at 0 so you'll skip the first element when accessing data[][j].

第三 - 你提到 Document.replaceText()。你不说你和它有什么问题,但它可能是你没有得到发生的,因为你没有找到文档中的文本替换。根据您的code,这里有一些可能的解释为:

Third - you mention Document.replaceText(). You don't say what problem you're having with it, but it could be that you're not getting replacements occurring because you're not finding the text in the document. Based on your code, here are some possible explanations for that:


  1. 错别字/不一致的情况。 六十,十三,十四 - 除非你的模板有确切的同样的错误,您的匹配将失败

  2. 空间 - 你正在寻找答案+ toText(J),你可能想答案+ toText(J)

  3. 连字符和放大器;更多的资本关注 - 数字往往是复姓,如二十一。此外,一在这种情况下不大写。但是,你的模版可能不同意 - 确保你匹配它

  1. Typos / inconsistent case. "SiXty", "Thirteen", "fourteen" - unless your template has the exact same mistakes, your matching will fail.
  2. Spaces - you're searching for "Answer"+toText(j), you probably want "Answer "+toText(j).
  3. Hyphens & more capitalization concerns - Numbers are often hyphenated, e.g. Twenty-one. Additionally, the "one" in this case is not capitalized. But your template may disagree - make sure you match it.

最后,你不用问什么,而是你贴code主要是什么 - 一个数字转换为英文字符串重新presentation。您可以并且应该简化您的 toText()功能。这种问题包含可以被利用来简化解决方案模式。你是一部分的方式出现 - 基本上,你有一组子的数字,可以只在自己的角度(零,一,二...... 19)pssed前$ P $,而另外一组复合材料( 20 [-blah],三十[-blah] ...)。因此该解决方案是分开这两个组,并使用简单的阵列来为相应的文本执行查找

Finally, what you don't ask, but what your posted code is mostly about - converting a number to an English string representation. You can and should simplify your toText() function. This kind of problem contains patterns that can be exploited to simplify the solution. You were part way there - basically, you've got one set of sub-numbers that can be expressed only in terms of themselves (zero, one, two...nineteen), and another set that are composites (twenty[-blah], thirty[-blah]...). So the solution is to separate those two groups, and use simply arrays to perform a lookup for the appropriate text.

function toText(num) {
  if (num >= 100) throw new Error("Too big");
  if (num < 0) throw new Error("Negative");
  if (num - Math.floor(num) > 0) throw new Error("Not Integer");

  var smallnums = ["Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
                 "Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"];
  var tens = ["","","Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","Ninety"];
  var s = "";

  // Is this number in the smallnums set?
  if (num < smallnums.length)
    s = smallnums[num];
  else {
    // No, so express the 'tens', then (maybe) the 'ones'.
    s += tens[Math.floor(num/10)];
    var remnant = num % 10;
    if (remnant > 0) s += "-" + smallnums[remnant].toLowerCase();
  }
  return s;
}

这篇关于对于循环迭代和replaceText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆