javascript - 使用 Google 脚本复制页面后,电子表格公式不会更新。我该如何解决这个问题

标签 javascript google-apps-script google-sheets

首先,这里有两个基本的电子表格,用于说明我正在讨论的内容:

第一页:https://docs.google.com/spreadsheets/d/1GZs_I9_beIO9xYBXatoxStRMmk9Aj3-UxPQpB9-iA-I/edit?usp=sharing

第二页:https://docs.google.com/spreadsheets/d/1R86_0SrAtyIJPAhMW0Ura-3gwOLn5l9u7Seuj2V0YCA/edit?usp=sharing

本质上,我正在使用 Google 脚本来“更新”标题为“要更新的副本”的电子表格。此副本将自动复制“要从中提取更新的副本”中的所有页面,删除“要更新的副本”上的当前页面,然后将复制的工作表重命名回之前的名称。这意味着我对“从中提取更新的副本”所做的任何更改都将自动在其他电子表格上更新,而无需重新分发电子表格。

当按下“更新页面”工作表上的按钮时,页面将成功传输。更改已成功“更新”到更新电子表格上。然而,代码中最先更新的页面有错误。任何具有从其他工作表获取数据的公式的单元格都会返回“#REF!”出现错误“无法解析工作表名称'在此处插入工作表名称”。”这意味着公式/单元格看不到脚本中新复制的工作表。我可以通过单击任何受此影响的单元格并按 Enter 键来解决这种情况 - 本质上是“刷新”单元格。不过,我正在一个具有 100 多个像这样的单元的项目上执行此操作,并且我只想按一个按钮即可执行此操作。

我应该尝试使用脚本解决此问题,还是更改以不同方式更新电子表格页面的方法?感谢所有提前提供的帮助!

这是我正在使用的脚本:

<code>
function Update() {
//Sets the ID of the page to update from
var ID = "1R86_0SrAtyIJPAhMW0Ura-3gwOLn5l9u7Seuj2V0YCA"
//Pulls the correct sheet using the ID
var source = SpreadsheetApp.openById(ID);
//Identifies the active spreadsheet in use to update. (The spreadsheet you pressed "Update" on)
var destination = SpreadsheetApp.getActiveSpreadsheet();
//Sheet to pull and copy to the current spreadsheet
var sheet1 = source.getSheetByName("1");
//Copying the sheet accross
sheet1.copyTo(destination);
//Identifies the old copy of the sheet
var sheet1 = destination.getSheetByName('1');
//Deletes the old copy of the sheet
destination.deleteSheet(sheet1);
//Gets the new copy of the sheet
var sheet1 = destination.getSheetByName('Copy of 1');
//Renames the new copy of the sheet
sheet1.setName("1")
//Repeating the same code with page 2
//Sheet to pull and copy to the current spreadsheet
var sheet1 = source.getSheetByName("2");
//Copying the sheet accross
sheet1.copyTo(destination);
//Identifies the old copy of the sheet
var sheet1 = destination.getSheetByName('2');
//Deletes the old copy of the sheet
destination.deleteSheet(sheet1);
//Gets the new copy of the sheet
var sheet1 = destination.getSheetByName('Copy of 2');
//Renames the new copy of the sheet
sheet1.setName("2")
//Repeating the same code
}
</code>

最佳答案

这与 Force formula refresh through Apps Script 相关

我遇到了完全相同的问题。我的大多数公式都是第一行或第二行中的 =ArrayFormula 公式。我的解决方案迭代给定电子表格的前两行中的所有单元格并重新计算所有公式。

这可能会被清理,以便仅修改单元格(如果它是公式)(而不是重置非公式的文本值)。还可以通过更改分配的范围对其进行修改,以在整个电子表格上运行。

您可以在将工作表复制到新位置后在工作表上运行此函数,也可以在所需工作表处于事件状态时调用 myFunction

function updateFormulasTwoRows(given_sheet) {
  var last_col = given_sheet.getDataRange().getLastColumn();
  var range = given_sheet.getRange(1,1,2,last_col);
  // contains empty strings or cell formulas
  var formulas = range.getFormulas();
  // contains text, formula errors, formula output
  var contents = range.getValues(); 

  // formulas[0][0].length > 1 ==> is a formula
  for (var r = 0; r < range.getHeight(); r++) {
    for (var c = 0; c < range.getWidth(); c++) {
      var cell = range.getCell(r+1,c+1);
      // clear only the contents, not notes or comments or formatting.
      cell.clearContent();
      if (formulas[r][c].length > 1) {
        // cell was a formula, so insert the formula back in place
        cell.setValue(formulas[r][c]);
      } else {
        // cell was not a formula, so insert the text content back in place
        cell.setValue(contents[r][c]);
      }
    }
  }
}

// run this after selecting a sheet to reevalute formulas on
function myFunction() {
  var curr_sheet = SpreadsheetApp.getActiveSheet();
  updateFormulasTwoRows(curr_sheet);
}

关于javascript - 使用 Google 脚本复制页面后,电子表格公式不会更新。我该如何解决这个问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26310308/

相关文章:

javascript - Google Apps 脚本 - 如果单元格 = 'yes',则使用 onEdit 运行脚本

google-apps-script - 使用 Google Apps 脚本中的 YouTube Data API v3 从播放列表中删除视频

javascript - 循环遍历单元格范围寻找值

javascript - 谷歌应用程序脚本 : how to copy array of objects to range?

google-sheets - 谷歌表单填充谷歌表中的平均持续时间

javascript - 当我将 "METHOD: REQUEST"添加到 iCalendar 时,Gmail 停止识别为事件

javascript - 使用 Google Apps 脚本将 HTTPPost 发送到 GCM 时出现错误=缺少注册

javascript - 在输入中添加值 - React

Javascript增加动画

javascript - 如何在发布 jQuery 表单时不重新加载 ASP 页面?