简体   繁体   English

简单的 Google 脚本和 Google 电子表格的问题

[英]Issue with simple Google Scripts and Google Spreadsheets

Update 2: It was a permissions issue.更新 2:这是一个权限问题。 I guess I have to duplicate all my scripts for each spreadsheet?我想我必须为每个电子表格复制所有脚本? Also I'm updating my code now that I've fixed it.此外,我正在更新我的代码,因为我已经修复了它。

Update: my code is wrong (I need to drop the second for loop I think) but my question is why the script isn't running on my sheets.更新:我的代码是错误的(我认为我需要删除第二个 for 循环)但我的问题是为什么脚本没有在我的工作表上运行。

I'm trying to write data to columns in a spreadsheet.我正在尝试将数据写入电子表格中的列。 Maybe I'm just having permissions issues?也许我只是有权限问题?

This is my first time using Google scripts and I'm not familiar with JavaScript either.这是我第一次使用 Google 脚本,我也不熟悉 JavaScript。 I have a spreadsheet with a lot of data and I successfully removed duplicates as defined by comparing the first two columns--no bugs, no issues, ran successfully first try.我有一个包含大量数据的电子表格,我通过比较前两列成功删除了定义的重复项——没有错误,没有问题,第一次尝试成功运行。 Now I'm trying to fill in data on two new, empty columns.现在我试图在两个新的空列上填写数据。

If column 2 is in the format "foo - bar", I want column 3 to be "foo" and col 4 "bar".如果第 2 列的格式为“foo - bar”,我希望第 3 列为“foo”,第 4 列为“bar”。 So I wrote this script:所以我写了这个脚本:

function parseTypes() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var data = sheet.getDataRange().getValues();
  var newData = new Array();
  for (i in data) {
    var row = data[i];
    var str = row[2];
    var strSplit = str.split(" - ");
    row[3] = strSplit[0];
    if (strSplit[1] == "" || strSplit[1] == null || row[4] == undefined) {
      row[4] = "";
    } else {
      row[4] = strSplit[1];
    }
    newData.push(row);
  }
  sheet.clearContents();
  sheet.getRange(1, 1, newData.length, newData[0].length).setValues(newData);
}

I debugged in and the string array strSplit is accurately being set to "foo,bar".我进行了调试,字符串数组 strSplit 被准确地设置为“foo,bar”。 My issue is that it's not actually writing the data to my spreadsheet.我的问题是它实际上并没有将数据写入我的电子表格。 I don't know if it's my code or if I'm just running the script wrong because I barely know what I'm doing.我不知道这是我的代码还是我运行的脚本错误,因为我几乎不知道我在做什么。

JavaScript use 0 based indexes but SpreadsheetApp use 1 based indexes. JavaScript 使用基于 0 的索引,但 SpreadsheetApp 使用基于 1 的索引。 This means that row[2] returns the value of column C not column B.这意味着 row[2] 返回的是 C 列的值,而不是 B 列的值。

You don't have to copy the script.您不必复制脚本。 The only thing you need to change is not using the SpreadsheetApp.getActiveSheet, but you have to use the SpreadsheetApp.openByUrl(' https://blalaallla ').getSheetByName('blalala') method.您唯一需要更改的不是使用 SpreadsheetApp.getActiveSheet,而是必须使用 SpreadsheetApp.openByUrl(' https://blalaallla ').getSheetByName('blalala') 方法。 This way you don't have to copy it.这样您就不必复制它。 Hope this helps.希望这可以帮助。

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

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