【问题标题】:Google Spreadsheet Script working with strings使用字符串的 Google 电子表格脚本
【发布时间】:2013-02-25 16:33:41
【问题描述】:

我有一个关于如何在 Google Script 中使用“字符串”类型的问题?

我有一列包含不同格式的网站,例如 http:///testsite.com/、http:///www.sitetest.net/、dot.anothersite.com、http:// /anysite.net/index.html 等等。

所以我想将这些记录自动格式化为一个模板:sitename.domain(例如 http://testsite.com/ = testsite.com || http:/强>/www.sitetest.net/ = sitetest.net)

我通过这个脚本得到这个信息:

var sheet = SpreadsheetApp.getActiveSheet();
  var sheetName = sheet.getName();
  var restrictSheet = "Summary";
  if (sheetName != restrictSheet){ // I don't need to modify first sheet from my spreadsheet
    var sheetRange = sheet.getActiveRange();
    var sData = sheet.getRange("A3:A"); // column with sites
    var TextArray = sData.getValues();
  }else Browser.msgBox("wrong sheet");
}

而且我不知道如何在 google 脚本中使用字符串。例如,在 C++ 中,我可以使用 String.Find() 或 String.Mid() 等字符串函数。但我在 Google Scripts 中看不到同等功能

【问题讨论】:

    标签: string google-apps-script google-sheets


    【解决方案1】:

    嗯...我已经找到了我自己的问题的答案:D

    Google Script 使用 JavaScript 中的 String 类 因此,来自 JS 的函数和方法也适用于 Google Scripts..

    http://www.w3schools.com/jsref/jsref_obj_string.asp

    也许它会对某人有所帮助。

    【讨论】:

      【解决方案2】:

      可能有点晚了..

      但很高兴知道许多常见的字符串对象方法在 Google App Script 中工作。 例如.endsWith.includes

      检查此堆栈溢出question

      这里有一些测试来验证

      function stringEndsWithTest(){
        var a = "abc@example.com";
        SpreadsheetApp.getUi().alert('endsWith method on a string in google app script '+ a.endsWith('.com')); 
      }
      
      function stringIncludesTest(){
        var a = "abc@example.com";
        SpreadsheetApp.getUi().alert('includes method on a string in google app script '+ a.includes('.com')); 
      }
      

      我在写这个答案时遇到了这个错误

      TypeError:找不到函数包含在对象 abc@example 中。 (第 19 行,文件“main”)

      作为上述方法的替代方法,对我有帮助的是“indexOf”方法

      function stringIndexOfTest(){
        var a = "abc@example.com";
        SpreadsheetApp.getUi().alert('indexOf method on a string in google app script '+ a.indexOf('.com')); 
      }
      

      我希望它可以帮助某人。

      【讨论】:

      • 问题是您必须使用b = a.toString(); 将变量“a”转换为字符串。然后就可以使用 String 类的方法了。
      • 这很有帮助,GAS 没有提供与 js 相同的 String 类方法。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多