【问题标题】:Why does Google sheet show old data when using IMPORTRANGE?为什么 Google 表格在使用 IMPORTRANGE 时会显示旧数据?
【发布时间】:2019-02-19 03:41:53
【问题描述】:

我创建了一个简单的 Google App 脚本,链接到一个简单的 google 表格。

Google 表格有 2 张表格:一张名为“Source”,另一张名为“Imported”:

https://docs.google.com/spreadsheets/d/1NODMmY8ua-enfjECumm76qszgogE2hyNkhygy8qe1uk/edit#gid=0

工作表“已导入”从工作表“源”中导入单元格“A1”中的数据。

但是,当我运行代码时,我注意到即使新导入的数据在“已导入”工作表中对人眼可见,代码也无法获取导入的数据。

复制问题的步骤:

  • 第 1 步:转到工作表“来源”并在单元格 A1 中输入国家名称“西班牙”。
  • 第 2 步:运行代码。
  • 第 3 步:检查日志。您会注意到,即使我们在代码中指定了“America”,代码仍然将名称记录为“Spain”,这是旧数据。

如果您第二次运行代码,一切都会按预期运行。要复制代码,您必须再次从第 1 步开始。

任何指向我可以调查的正确方向的指针?

我的代码供您快速参考:

function Stack_Overflow_Function() {
    var name = "America";
    SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Source").activate();
    var ss_source = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
    ss_source.getRange(1,1).setValue(name);
    Utilities.sleep(1000)

    SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Imported").activate();
    var ss_imported = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
    var lr = ss_imported.getLastRow();

    var current_name = ss_imported.getRange(1,1).getValue();
    Logger.log('The Current name in Imported sheet is '+current_name);


}

编辑:很抱歉没有提及,数据是使用标准函数 IMPORTRANGE('Google sheet URL',Source!A1:A1) 在“已导入”工作表中使用 IMPORTRANGE 导入的

【问题讨论】:

  • 您似乎没有将任何数据从 Source 复制到 Imported?我误解你的问题了吗?
  • 抱歉没有提及,数据是在“已导入”工作表中使用 IMPORTRANGE 导入的

标签: google-apps-script importrange


【解决方案1】:

我在整个过程中都对代码进行了注释,但似乎您从未将值复制到“导入”表中。此外,建议您仅在要更改向用户显示的内容时才使用.activate(),否则应避免仅获取电子表格和工作表对象。

function stackOverflowFunction() {
    var name = "America";
    /* Consider the method I've used below for getting the Spreadsheet and Sheet Object.
       You should only use .activate() when you want to change what the user is looking at.

    SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Source").activate();
    var ss_source = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
    */

    var spread = SpreadsheetApp.getActiveSpreadsheet();
    var ss_source = spread.getSheetByName("Source");
    var sourceRange = ss_source.getRange(1,1); //Get the soure range.

    sourceRange.setValue(name); //Sets the value of the source range.

    Utilities.sleep(1000);

    var ss_imported = spread.getSheetByName("Imported"); //Get the imported sheet
    var importedRange = ss_imported.getRange(1, 1); //Gets the imported sheet range.

    importedRange.setValue(sourceRange.getValue()); //Gets source sheet value and uses it to set the imported sheet value.

    // var lr = ss_imported.getLastRow(); //Not sure what this was for but getLastRow() gets an Integer representing the last row in the sheet.

    //Now lets check the imported range value...
    var current_name = ss_imported.getRange(1,1).getValue();
    Logger.log('The Current name in Imported sheet is '+ current_name);
}

如果我误解了您的问题,请告诉我。

【讨论】:

  • 谢谢Chris,我之前可能不太清楚:“Imported”工作表只需要使用IMPORTRANGE函数从工作表源导入数据。如果我理解正确,您的代码是将源工作表中的数据硬编码到工作表目标工作表中。我想保留 IMPORTRANGE 函数,因为我想从源工作表到目标工作表的数据非常大。
猜你喜欢
  • 1970-01-01
  • 2021-07-27
  • 2019-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多