Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 创建范围并重新指定给其他图纸_Google Apps Script_Google Sheets - Fatal编程技术网

Google apps script 创建范围并重新指定给其他图纸

Google apps script 创建范围并重新指定给其他图纸,google-apps-script,google-sheets,Google Apps Script,Google Sheets,我构建了一个范围,试图将其从一张图纸重新分配到另一张图纸,但由于错误而失败 function test() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var sourceSheet = ss.getSheetByName('Sheet1'); var destinationSheet = ss.getSheetByName('Sheet2'); thisWeek = 37; var firstWeek = [];

我构建了一个范围,试图将其从一张图纸重新分配到另一张图纸,但由于错误而失败

function test() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sourceSheet = ss.getSheetByName('Sheet1');
  var destinationSheet = ss.getSheetByName('Sheet2');

  thisWeek = 37;
  var firstWeek = [];
  var unprotectRange = [];
  firstWeek[0] = sourceSheet.getRange('E6:E12');
  firstWeek[1] = sourceSheet.getRange('F8:F10');

  const columnsPerWeek = 51;

  for (var k = thisWeek - 1; k < 53; k++) {
    for (var l = 0; l < firstWeek.length; l++) {
      if (
        sourceSheet
          .getRange(firstWeek[l].offset(0, columnsPerWeek * k).getA1Notation())
          .isBlank()
      ) {
        unprotectRange.push(
          sourceSheet.getRange(
            firstWeek[l].offset(0, columnsPerWeek * k).getA1Notation()
          )
        );
      }
    }
  }

  var range2 = destinationSheet.getRange(unprotectRange.getA1Notation());
  var protection = destinationSheet.protect().setDescription('Maintain');
  protection.setUnprotectedRanges(range2);
}
  • unprotectRange
    是源工作表中的范围数组。它们必须是字符串(一个符号)。只按A1符号

  • 使用以下命令将字符串(A1符号)转换为目标工作表的范围

  • 然后将接受此数组


你能简单解释一下哪些范围是不受保护的吗?您只是想取消对空白区域的保护吗?另外,
保护
未定义的
。我有X个目标表(受保护),我需要在所有目标表中取消某些范围的保护。基于什么标准?团队中的每个球员都有自己的表(目标表),数组第一周包含所有范围(每个范围代表本赛季的一场比赛)。第一周的那些范围是空的(因为那场比赛我们还没有一个完整的团队),我希望玩家能够在他们自己的工作表中进行编辑(基本上输入是/否/可能)。
unprotectRange
是一个数组。您需要在数组中选择一个元素,如
取消保护范围[0]
。感谢您的帮助。不幸的是,我在语句
const ranges2=unprotectRange.map(a1Notation=>destinationSheet.getRange(a1Notation))中得到一个错误“Range not found”
@mortpiedra Log
unprotectRange
并查看
destinationSheet
是否包含
unprotectRange
uhu中的所有范围。我不确定你的意思。在循环中,我放入
Logger.log(sourceSheet.getRange(第一周[l].offset(0,columnsPerWeek*k).getA1Notation()).getA1Notation()我得到了E6:E12 F8:F10 F6:F12 G8:G10 G6:G12 H8:H10I也改变了:
本周=1;const columnsPerWeek=1;对于(变量k=thisWeek-1;k<3;k++)
@mortpiedra
E6:E12 F8:F10 F6:F12 G8:G10 G6:G12 H8:H10
目标表中是否提供了所有这些范围?
var range2 = destinationSheet.getRange(unprotectRange.getA1Notation());
/*...*/
if (
  sourceSheet
    .getRange(firstWeek[l].offset(0, columnsPerWeek * k).getA1Notation())
    .isBlank()
) {
  unprotectRange.push(
    firstWeek[l].offset(0, columnsPerWeek * k).getA1Notation()/*Modified*/
  );
}
/*...*/
const ranges2 = unprotectRange.map(a1Notation => destinationSheet.getRange(a1Notation));
const protection = destinationSheet.protect().setDescription('Maintain');
protection.setUnprotectedRanges(ranges2);
protection.removeEditors(protection.getEditors())