George Huang
George Huang

Reputation: 5

SuiteScript 2.0 : i use context.response.writeFile(file) to export data to excel,but it return String

I add a button.when I click the button ,it will execut this function.

function onRequest(context) {
    log.debug('exportTest');
    var stringInput = 'Hello World\nHello World';
    var base64EncodedString = encode.convert({
        string : stringInput,
        inputEncoding : encode.Encoding.UTF_8,
        outputEncoding : encode.Encoding.BASE_64
    });
    var fileUrl = file.create({
        name : 'test.txt',
        fileType : file.Type.PLAINTEXT,
        contents : base64EncodedString
    });
    log.debug('fileUrl',fileUrl);
    context.response.writeFile({
        file : fileUrl
    });
}

I want to get a file called 'test.txt',but it return a String. enter image description here

Upvotes: 0

Views: 2263

Answers (2)

George Huang
George Huang

Reputation: 5

I know how to do it now. 1.add a button

function beforeLoad(scriptContext) {
    if(scriptContext.type == scriptContext.UserEventType.VIEW){
        var form = scriptContext.form;
        form.addButton({
            id: "custpage_export_test",
            label: "Export Test",
            functionName: 'exportExcel'
        });
        form.clientScriptModulePath = './export_test.js';            
    }
}

2.the export_test.js

function exportExcel(context) {
    var suiteletURL = url.resolveScript({
        scriptId:'customscript_export_excel',
        deploymentId:'customdeploy_export_excel',
        params:context
    })
    var downloadLink = document.createElement('a');
    downloadLink.href = suiteletURL;
    document.body.appendChild(downloadLink);
    downloadLink.click();
    document.body.removeChild(downloadLink);
}

3.the export_excel.js

function onRequest(context) {
    var response = context.response;
    log.debug('exportTest');
    var stringInput = 'Hello World\nHello World';
    var excelFile = file.create({
        name: 'test.txt',
        fileType: file.Type.PLAINTEXT,
        contents: stringInput
    });
    response.writeFile(excelFile);
}

Upvotes: 0

Krypton
Krypton

Reputation: 5231

It appears to be returning exactly what you're asking for - that is, 'Hello World\nHello World' encoded as a base64 string. To display the original text you would need to decode again, or for this example you could just skip the encoding as it's only text anyway.

Upvotes: 1

Related Questions