我已经创建了一个链接到简单Google表格的简单Google App脚本。
Google工作表有2个工作表:一个工作表名为“来源”,第二个工作表名为“已导入”:
https://docs.google.com/spreadsheets/d/1NODMmY8ua-enfjECumm76qszgogE2hyNkhygy8qe1uk/edit#gid=0
工作表“已导入”从工作表“源”中导入单元格“ A1”中的数据。
但是,当我运行代码时,我注意到即使人为在“导入的”工作表中看到新导入的数据,代码也无法拾取导入的数据。
复制问题的步骤:
如果第二次运行代码,则一切正常。要复制代码,您将不得不再次从步骤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在“已导入”工作表中导入数据。答案 0 :(得分:0)
我已经注释了整个代码,但似乎您从未将值复制到“导入”表中。此外,建议仅在要更改向用户显示的内容时使用.activate()
,否则应避免仅获取Spreadsheet和Sheet对象。
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);
}
如果我误解了您的问题,请告诉我。