Working with GridJs storage

Working With GridJs Storage

The General File Process

After importing a spreadsheet file, GridJs will create a cache file with the specified UID in the Config.file_cache_directory folder, using the format of Aspose.Cells.SaveFormat.Xlsx.

GridJs will also save all shapes/images to a zip‑archive file in the Config.file_cache_directory folder for later display of shapes/images in the client UI.

After every update operation in the client UI— for example, setting a cell value, setting a cell style, etc.— the GridJs client‑side JavaScript will trigger an Ajax call to perform an UpdateCell operation.

In this action, a save back to the cache file will occur during the UpdateCell method.

# update action : /GridJs2/UpdateCell
@app.route('/GridJs2/UpdateCell', methods=['POST'])
def update_cell():
    # get request param
    p = request.form.get('p')
    uid = request.form.get('uid')

    gwb = GridJsWorkbook()
    ret = gwb.update_cell(p, uid)

    return Response(ret, content_type='text/plain; charset=utf-8')

How to Get the Updated Result File

1. A Specified UID for File

Make sure there is a specified mapping correspondence between the file and the UID; you can always get the same UID for a specified file name, not from random generation. For example, just using the filename is OK.

...
# get JSON info from: /GridJs2/DetailFileJsonWithUid?filename=&uid=
@app.route('/GridJs2/DetailFileJsonWithUid', methods=['GET'])
def detail_file_json_with_uid():
    filename = request.args.get('filename')
    uid = request.args.get('uid')
    if not filename:
        return jsonify({'error': 'filename is required'}), 400
    if not uid:
        return jsonify({'error': 'uid is required'}), 400
    gwb = GridJsWorkbook()
    file_path = os.path.join(FILE_DIRECTORY, filename)

    # check file
    if not os.path.isfile(file_path):
        return jsonify({'error': 'file not found:' + file_path}), 404
    try:
        sb = gwb.get_json_str_by_uid(uid, filename)
        if sb is None:
            gwb.import_excel_file(uid, file_path)
            sb = gwb.export_to_json(filename)
        response = Response(sb, status=200, mimetype='text/plain')
        response.headers['Content-Type'] = 'text/plain; charset=utf-8'
        return response

    except Exception as ex:
        return jsonify({'error': str(ex)}), 500

2. Sync with Client UI Operations

Actually, for some client UI operations— for example, switching the active sheet to another, changing the image position, rotating/resizing an image, etc.— the UpdateCell action will not be triggered. Thus, if we want to get the updated file exactly as the client UI shows, we need to perform a merge operation before the save action to sync those client UI operations.

// in the JS
function save() {
    if (!xs.buffer.isFinish()) {
        alert('updating is in progress, please try later');
        return;
    }
    let datas = xs.datas;
    delete datas.history;
    delete datas.search;
    delete datas.images;
    delete datas.shapes;

    var jsondata = {
        sheetname: xs.sheet.data.name,
        actrow: xs.sheet.data.selector.ri,
        actcol: xs.sheet.data.selector.ci,
        datas: xs.getUpdateDatas(),
    };

    const data = {
        p: JSON.stringify(jsondata),
        uid: uniqueid,
    };
    // ... continue to do AJAX POST to trigger controller action
}
# in the download route action
gwb = GridJsWorkbook()
gwb.merge_excel_file_from_json(uid, p)
# after merge, save to cache (or to a stream, whatever you want); here we just save to cache
gwb.save_to_cache_with_file_name(uid, filename, None)

3. Get the File from Cache

For example, in the getfile action you can retrieve the file from the cache directory by UID.

mimetype = guess_mime_type_from_filename(filename)
file_path = os.path.join(
    Config.file_cache_directory,
    uid.replace('/', '.') + "." + filename
)
# check file
if os.path.isfile(file_path):
    return send_file(
        file_path,
        as_attachment=True,
        download_name=filename,
        mimetype=mimetype
    )

For more detailed information, you can check the example here:
https://github.com/aspose-cells/Aspose.Cells.Grid-for-Python-via-.NET/tree/main/Examples.GridJs