将绝对路径转换为相对路径时出错

时间:2014-05-05 13:27:02

标签: javascript excel relative-path absolute-path

我正在使用需要打开csv文件的javascript代码,但我收到错误为“ ThisWorkbook未定义”。不确定是什么错误。请帮忙!

var xl = new ActiveXObject("Excel.Application");
    xl.Visible = true;
    var wb = xl.Workbooks.Open(ThisWorkbook.Path & "\\" & "temp.csv");
    var sheet = wb.ActiveSheet;
    wb.Worksheets.Add(after=wb.Sheets(wb.Sheets.Count));
    var pws = wb.ActiveSheet;
    pws.Name="temp_pivot";
    var pvtTable = pws.PivotTableWizard(1, varSource=wb.sheets("temp").Range("A1").CurrentRegion);
    pvtTable.PivotFields("DECISIONYEAR").Orientation = 1;
    pvtTable.PivotFields("DECISIONMONTH").Orientation = 1;
    pvtTable.PivotFields("APPLICANT").Orientation = 4;
    pvtTable.PivotFields("SUM OF APPLICANT").Function=-4157;
    pvtTable.PivotFields("SUM OF APPLICANT").Caption="Applicants";
    pvtTable.PivotFields("APPROVED").Orientation = 4;
    pvtTable.PivotFields("SUM OF APPROVED").Function=-4157;
    pvtTable.PivotFields("SUM OF APPROVED").Caption="NAA";
    pvtTable.PivotFields("SOURCE").Orientation = 3;
    pvtTable.PivotFields("PRODUCTCOLOR").Orientation = 3;
    pvtTable.PivotFields("PRODUCT").Orientation = 3;
    pvtTable.PivotFields("SUB_CHANNEL_CADM").Orientation = 3;
    pvtTable.PivotFields("CHANNEL_CADM").Orientation = 3;
    pvtTable.PivotFields("MARKET").Orientation = 3;
    pvtTable.PivotFields("OFFER_TYPE").Orientation = 3;
    pvtTable.DataPivotField.Orientation=2;
    pws.Columns.Autofit;
    pws.Rows.Autofit;
    xl.DisplayAlerts =0;
    xl.DisplayAlerts = 1
    xl.CutCopyMode = 0;
    xl.EnableEvents = 0;
    xl = null;

2 个答案:

答案 0 :(得分:0)

\"被视为字符串文字中的转义引号。这意味着您的"\"字符串未关闭。

替换

"\" & "temp.csv"

"\\" & "temp.csv"

使用正确的代码编辑器,代码的颜色会暗示问题所在(如本答案所示)。

答案 1 :(得分:0)

"\"替换为"\\"

"\"中,斜杠会转义第二个引号,这意味着您的字符串不会在那里终止。

要让字符串包含反斜杠,您必须转义反斜杠("\\"),因此编译器/解释器不会尝试将其解释为转义符。