0

如果我未能描述我面临的问题或我偶然发现它的背景,我应该首先道歉。如果有任何其他信息可以帮助您了解正在发生的事情,请告诉我。

上下文: 我在一张电子表格中有两张纸:“清单”,它应该作为一个表格工作,而“编辑清单”,顾名思义,将用于编辑前者的格式。这个想法是,一旦用户完成编辑宏,然后只需将新版本复制到旧版本上即可替换以前的形式。

问题: 通过脚本使用 Range.copyTo() 函数不会复制整个新表单,缺少一整列,尤其是格式和合并范围。请在下面找到截图

新表格的示例,即尚未复制。 使用 Range.copyTo() 复制新表单的结果

代码

/**
 *   Updates the checklist form after edditing.
 */
function update_checklist(){

  // First we clean whatever used to be the checklis
  del_checklist_sections();

  // Now we gather the new checklist into a range
  new_checklist = get_edit_checklist_form_range();

  // We now have to make room for the new list on the Checklist Sheet
  b_row = spreadsheet.getRangeByName("Checklist!"+NAMED_RANGES.form_start).getRow();
  f_form.insertRowsAfter(b_row, new_checklist.getNumRows());

  //new_checklist.copyFormatToRange(f_form, 1, new_checklist.getLastColumn(), b_row+1, b_row+1+new_checklist.getNumRows());

  new_checklist.copyTo(get_checklist_form_range(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
  //new_checklist.copyTo(get_checklist_form_range(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);

}

/**
 *   Correctly grabs the sections and tasks range in the checklist sheet. 
 *
 * The correct range doesn't include the first and last rows of the named range. 
 *
 * @return {Range} the corresponding range.
 */
function get_checklist_form_range(){

  full_range = spreadsheet.getRangeByName("Checklist!"+NAMED_RANGES.full_form);
  if (full_range.getNumRows() > 2){

    // We are adding one so we won't consider the initial row in the range, and subtracting to so we won't consider the first and last.
    return f_form.getRange(full_range.getRow()+1, full_range.getColumn(), full_range.getNumRows() - 2);
  }else{
    return 0
  }

}

function get_edit_checklist_form_range(){

  full_range = spreadsheet.getRangeByName("Edit Checklist!"+NAMED_RANGES.full_form)
  // We are adding one so we won't consider the initial row in the range, and subtracting to so we won't consider the first and last.
  //  return f_form.getRange(full_range.getRow()+1, full_range.getColumn(), full_range.getNumRows() - 2)
  return e_form.getRange(full_range.getRow()+1, full_range.getColumn(), full_range.getNumRows() - 2);

}

注意:为了更容易找到范围,我使用命名范围来指定工作表的各个部分。

更新:

  1. 我试图记录一个宏来执行复制和粘贴任务,并且使用生成的代码工作得很好。我将我的一段代码与自动生成的代码进行了比较,唯一的区别是范围的编写方式,但最终它们应该是相同的。
  2. 我尝试先粘贴格式,然后再粘贴值;它没有用。
4

1 回答 1

0

我花了一段时间才回到这个项目,但我们开始吧:

问题是我如何获得范围。事实证明,我没有选择要复制和粘贴的所有数据。

于 2019-03-12T21:42:30.470 回答