Javascript 使用loop-in-cloud函数将json数据呈现到DOCX表中
这是一段代码,其中我们为申请者提供json,表标题为value,索引为key。 我得到了这个错误:----Javascript 使用loop-in-cloud函数将json数据呈现到DOCX表中,javascript,node.js,reactjs,google-cloud-functions,docx,Javascript,Node.js,Reactjs,Google Cloud Functions,Docx,这是一段代码,其中我们为申请者提供json,表标题为value,索引为key。 我得到了这个错误:---- TypeError:无法读取新表({… 此方法用于从json数据添加表行: createNewRow = (title,index) => { return new TableRow({ children:[ new TableCell({ children: [
TypeError:无法读取新表({… 此方法用于从json数据添加表行:
createNewRow = (title,index) => {
return new TableRow({
children:[
new TableCell({
children: [
new Paragraph({
text:index,
// heading:HeadingLevel.HEADING_1,
}),
],
verticalAlign: VerticalAlign.LEFT
}),
new TableCell({
children: [
new Paragraph({
text:title,
// heading:HeadingLevel.HEADING_1,
}),
],
verticalAlign: VerticalAlign.LEFT
}),
]
})
}
创建表方法:
createDataTable = (applicantData) => {
console.log("recived data"+applicantData);
return new Table({
width: {
size: 4535,
type: WidthType.DXA,
},
rows:[
Object.entries(applicantData).forEach(([key, value]) => {
console.log("key "+key+"\tvalue "+value);
createNewRow(value,key);
})
]
});
}
这是云的主要功能
exports.genrateDOCXFile = functions.https.onRequest(async (req, res) => {
res.set("Access-Control-Allow-Origin", "*");
var applicantData = req.body.applicantData;
// var phoneNum = req.body.phoneNumber;
// var db = admin.firestore();
console.log("applicantData:"+applicantData);
const doc = new Document({
sections: [{
properties: {},
children: [
new Paragraph({
alignment: AlignmentType.CENTER,
heading:HeadingLevel.HEADING_1,
children: [
new TextRun({
text: "Satya Financing Agency",
bold: true,
underline: {
type: UnderlineType.DOUBLE,
color: "990011",
},
allCaps: true,
}),
],
}),
createDataTable(applicantData),
],
}],
});
const b64string = await Packer.toBase64String(doc);
res.setHeader('Content-Disposition', 'attachment; filename=My Document.docx');
res.send(Buffer.from(b64string, 'base64'));
});