问题
The problem:
When using IMPORTRANGE
to pull data from a different spreadsheet, cells in the origin spreadsheet that contain a formule containing VLOOKUP often (but not always) return #N/A (ERROR: Did not find value 'xxx' in VLOOKUP evaluation)
. In the origin sheet the formulas are calculated correctly and showing a value though. This does not always happen, sometimes it does pull in the proper value.
The intent:
To take data from different spreadsheets, combine them in a different spreadsheet and do some calculations on them. And then pull info from this calculation spreadsheet (or from multiple calculation spreadsheets) into a reporting spreadsheet for some further calculations and formatting.
The set-up:
There are several source data spreadsheets,say dataspreadsheet1
, dataspreadsheet2
and dataspreadsheet3
. A calculation spreadsheet (calcspreadsheet
) is created that creates a copy of sheet1
in each of the data spreadsheets and names these sheets datasheet1
, datasheet2
and datasheet3
respectively. The IMPORTRANGE
statement used for this is created as follows: importrange(+VLOOKUP("dataspreadsheet1",filelist!A1:C1000,3,FALSE),"sheet1!a1:Z1000")
where
filelist!A1:C1000
is a sheet in calcspreadsheet
that contains Name, Type and Id in respective columns.
The values in each of these sheets datasheet1-3
are then used for calculations in another sheet, calcsheet1
in the calcspreadsheet
. As the main goal of this is to add up daily values from the 3 dataspreadsheets
, but those sourcesheets
do not all have the same data on the same line a VLOOKUP
is used again to make sure additions for a date use the line for the date in datasheet1-3
regardless of its row number. E.g. VLOOKUP($A11,'datasheet1'!$A:$P,4) + VLOOKUP($A11,'datasheet2'!$A:$P,4) + VLOOKUP($A11,'datasheet3'!$A:$P,4)
where column A is the date column in all sheets.
This appears to work fine, although upon opening calcspreadsheet
it can take a long time for it to go through an update, during which time lots of #N/A
's are displayed. Eventually it comes right though.
The problem arise when a reportspreadsheet
is created that in turn used an IMPORTRANGE
call to pull the info from calcsheet1
in order to be able to work with it. This often, but not always, results in the problem states at the start. The IMPORTRANGE
call in this reportspreadsheet
is generated in a similar way as that in the calcspreadsheet
: =importrange(+VLOOKUP(calc!B1,sheetcodes!A1:C3000,3,FALSE),"sheet1!a1:Z1000")
where calc!B1
contains the name of the source spreadsheet (in this calc
that would be 'calcspreadsheet' and sheetcodes!A1:C3000
again contains a list of sheets with Name
, Type
and Id
in respective columns
A work-around I tried:
What I did notice that IMPORTRANGE
works better on cells that do not contain VLOOKUP
So I tried to copy the content of calcsheet
to another sheet in calcspreadsheet
, called exportsheet
but having only the values in there, not formulas and then use IMPORTRANGE
on this exportsheet
. The copy script used is as follows:
function exportPrep() {
// Get the active spreadsheet and the active sheet
//var ss = SpreadsheetApp.getActiveSpreadsheet();
//var sheet = ss.getSheetByName("stream");
//sheet.activate();
var source = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("calcsheet");
var sourceDataRange = source.getDataRange();
var sourceSheetValues = sourceDataRange.getValues();
var sourceRows = sourceDataRange.getNumRows();
var sourceColumns = sourceDataRange.getNumColumns();
var destination = SpreadsheetApp.getActiveSpreadsheet();
SpreadsheetApp.setActiveSheet(destination.setActiveSheet("exportsheet"));
destination.getDataRange().offset(0, 0, sourceRows, sourceColumns).setValues(sourceSheetValues);
}
This seemed to work, but unfortunately the copy script used to copy the value of calcsheet into exportsheet now showed the same behaviour, it would sometimes work and sometimes give the #N/A
and so leaves me with the same problem.
My questions:
I've read various posts with similar issues and responses that mentioned this function was temperamental or had a bug. Other stated it is not possible to use dynamic references in IMPORTRANGE
. Given that it sometimes works and sometimes not I suspect the function itself might be correct but that there are sync or time-out issues in the set-up.
How can I set up the above functionality. Either with the use of
IMPORTRANGE
andVLOOKUP
at all with some changes/additions, or built in a different way from the get-go.
回答1:
So I've not done this with importrange but I when I have this issue with Vlookup I wrap the vlookup in an IF and test for the #N/A.
Try something like: =IF(ISNA(VLOOKUP(...)),"",VLOOKUP(...))
回答2:
=IFERROR(VLOOKUP(B81,'KAM_Q3_OPPS STAGE_(H/I/L/M/N)'!$F$3:$G$111,2,False),)
This is the best way I found to do it and the last two values [ , ) ]
make the new output, in this case it is nothing.
IF you wanted 0 put in then you would do [ ,0) ]
来源:https://stackoverflow.com/questions/26099174/how-to-link-various-google-spreadsheets-using-importranges-that-contain-vlookup