【问题标题】:Duplicate some rows in a sheet range using google apps script使用谷歌应用脚​​本在工作表范围内复制一些行
【发布时间】:2020-10-15 18:27:11
【问题描述】:

我的脚本中有一本字典 key -> {list of values}

我有一张带有这些标题的表格:

key, name

我想遍历工作表中的某个范围并添加值行。

说:

k1 -> val1, val2

k2 -> val3

和:

我看到了这个post

问题是在迭代时我改变了范围。 我怎样才能有效地做到这一点?

    var rangeValues = searchRange.getValues();
  // Loop through array and if condition met, add relevant
  // background color.
  for ( i = 0; i < lastColumn - 1; i++){
    for ( j = 0 ; j < lastRow - 1; j++){
      var kw_data = kw_to_label[rangeValues[j][i]];

【问题讨论】:

  • 您确定您的图形没有问题吗?你的例子中 key1 怎么有 value3?

标签: javascript google-apps-script google-sheets google-sheets-formula google-sheets-api


【解决方案1】:

我在没有明确说明的情况下尝试了一些东西。我认为您的值列表是在您的脚本中硬编码的。基本上你要做的是创建一个新的结果数组并将新值设置为所需的范围。 这没有经过测试,但这个想法或多或少:

   const dictionnary = {
      'k1': ['val1', 'val2'],
      'k2': ['val3']
    }
    //range in the graphic where you have key and name so always 2 columns
    const values = searchRange.getValues()
    //future values to populate the spreadsheet with will be a 2D arrya at the end
    const newValues = []
    //loop through range where there is name and key
    for (let row = 0; row < values.length; row++)
      //check if key is inside the dictionnary
      if (values[row][0] in dictionnary){
        //get the list of values inside your dictionnary by key
        const tmpArr = dictionnary[values[row][0]]
        //loop through list
        for (let lign = 0; lign < tmpArr.length; lign++)
          //create the new row and push it to newValues
          newValues.push([values[row][0], values[row][1], tmpArr[lign]])
      }
    //generate the desired range of the size of newValues
    const newRange = sheet.getRange(desiredRowIndex, desiredColumnIndex, newValues.length, newValues[0].length);
    //set values inside range
    newRange.setValues(newValues)

说明:

  • 遍历源范围内的所有行。
  • 对于每一行,检查对应的键是否在脚本对象中。
  • 对于此键的值数组中的每个元素,将新的数组元素推送到二维数组 (newValues)。
  • 通过setValues(values) 将newValues 写入工作表,其中范围的尺寸基于二维数组的尺寸。

【讨论】:

  • 您需要为newRange指定列数(即添加newValues[0])。除此之外,这是一个有效的解决方案。
  • 当我看到你的解决方案时,我已经准备好了,所以我还是发布了它,作为一种可能的替代方案。它非常相似,但首先遍历对象键,并使用for...in、find 和forEach 而不是for 和if。
【解决方案2】:

问题:

  • 工作表中有一个范围,每行有两列,例如 key | name。
  • 脚本中有一个具有不同keys 的对象(其中每个key 对应于前面提到的范围内的一个key)以及每个不同key 的值数组,例如"key1": ["value1", "value2", ...]。
  • 您想修改原始范围,以便为键中的每个value 写入一个新行,例如key1 | name1 | value1、key1 | name1 | value2 等。

解决办法:

您可以执行以下操作:

  • 使用for... in遍历对象中的所有键。
  • 对于每个键,遍历对应数组中的所有值,使用forEach 和push 一个新的数组元素(具有值key, name, value)到一个二维数组(在下面的示例中称为newValues) .
  • 遍历每个键后,获取二维数组newValues的维度的范围,并将newValues中的数据写入此范围,使用setValues(values)。

代码示例:

function modifyRange() {
  const sheet = SpreadsheetApp.getActiveSheet();
  const firstRow = 1; // Please change according to your preferences
  const firstCol = 1; // Please change according to your preferences
  const searchRange = sheet.getRange(firstRow,firstCol,sheet.getLastRow(),2);
  const object = {
    "key1": ["value1", "value2"],
    "key2": ["value3"],
    // ...
  }
  const values = searchRange.getValues();
  const newValues = [];
  for (const key in object) {
    const row = values.find(row => row[0] === key);
    if (row) {
      object[key].forEach(keyValue => {
        newValues.push([row[0], row[1], keyValue]);
      });
    }
  }
  sheet.getRange(firstRow,firstCol,newValues.length,newValues[0].length)
       .setValues(newValues);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-16
    • 2021-10-11
    • 2022-12-03
    • 1970-01-01
    • 2019-04-27
    • 2023-03-12
    • 2014-09-19
    相关资源
    最近更新 更多