Scripting Indesign: alternating paragraph styles - scripting

How do I do this:
I have a document with two alternating paragraph styles, s1 and s2. s1 is followed by s2 and vice versa. This works fine with the user interface. But how does it work with scripting?
In the following code sample all text gets formatted as paragraph style s1 instead of alternating the styles. When you add paragraphs using the user interface, the paragraph format alternates as desired.
var myDocument = app.documents.add(true);
var s1 = myDocument.paragraphStyles.add({name:'one'});
var s2 = myDocument.paragraphStyles.add({name:'two'});
with (s1) {nextStyle = s2;}
with (s2) {nextStyle = s1;}
var myTextFrame = myDocument.spreads.lastItem().pages.lastItem().textFrames.add();
myTextFrame.geometricBounds = ["20mm","20mm","100mm","100mm"];
myTextFrame.parentStory.insertionPoints.item(0).appliedParagraphStyle = s1;
myTextFrame.contents = "abc\rdef\rghi\rklm\r";

You can reach the goal by applying objectStyle at the end (key property ==> applyNextParagraphStyle: true)
something like:
var
myDocument = app.documents.add(true),
s1 = myDocument.paragraphStyles.add({name:'one'}),
s2 = myDocument.paragraphStyles.add({name:'two'}),
objSt = myDocument.objectStyles.add({
name: 'AlternateParas',
enableParagraphStyle: true,
appliedParagraphStyle: s1,
applyNextParagraphStyle: true
}),
myTextFrame = myDocument.pages.lastItem().textFrames.add({
contents: 'abc\rdef\rghi\rklm\r',
});
with (s1) {nextStyle = s2;}
with (s2) {nextStyle = s1;}
myTextFrame.appliedObjectStyle = objSt;

Related

how to use mpandroidchart to draw a barchart with intervals

The y-axis of the official case starts from 0
I hope to realize the image function through mpandroidchart
You can accomplish this using a stacked bar chart and setting the color of the bottom bar to transparent.
For example:
// Make some fake data - the first array will be the offset,
// with a transparent color. The second is the height of the bar
val offset = listOf(42f, 55f, 55f, 80f, 55f, 110f)
val amount = listOf(50f, 50f, 50f, 50f, 50f, 55f)
val entries = offset.zip(amount).mapIndexed { i, oa ->
BarEntry(i.toFloat(), listOf(oa.first, oa.second).toFloatArray())
}
// To make the bars different colors, make multiple BarDataSets
// instead of just one
val barDataSet = BarDataSet(entries,"data")
barDataSet.colors = listOf(Color.TRANSPARENT, Color.CYAN)
barDataSet.setDrawValues(false)
barChart.setTouchEnabled(false)
barChart.description.isEnabled = false
barChart.legend.isEnabled = false
val yAxis = barChart.axisLeft
yAxis.isEnabled = true
yAxis.axisMinimum = 40f
yAxis.axisMaximum = 160f
yAxis.granularity = 20f
yAxis.textSize = 24f
yAxis.setLabelCount(6, true)
yAxis.setDrawAxisLine(false)
yAxis.gridLineWidth = 1f
yAxis.setDrawGridLines(true)
barChart.axisRight.isEnabled = false
barChart.xAxis.isEnabled = false
barChart.data = BarData(barDataSet)
The result:

docx4j how to insert image into table cell

I can't insert image into table cell using docx4j using following code:
WordprocessingMLPackage wordPackage = WordprocessingMLPackage.createPackage(PageSizePaper.A4,true);
ObjectFactory factory=Context.getWmlObjectFactory();Tbl table = factory.createTbl();
Tr tableRow = factory.createTr();
byte[] imageBytes = Base64.getDecoder().decode(t.getBase64Image());
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordPackage, imageBytes);
Inline inline = imagePart.createImageInline("image", "image", 0, 1, false);
P celPar = addInlineImageToParagraph(inline, factory);
Tc tableCell = factory.createTc();
tableCell.getContent().clear();
tableCell.getContent().add(celPar);
tableRow.getContent().add(tableCell);
wordPackage.getMainDocumentPart().addObject(table);
private P addInlineImageToParagraph(Inline inline, ObjectFactory factory) {
P paragraph = factory.createP();
R run = factory.createR();
paragraph.getContent().add(run);
Drawing drawing = factory.createDrawing();
run.getContent().add(drawing);
drawing.getAnchorOrInline().add(inline);
return paragraph;
}
Word has problem displaying image. I realy don't know where's problem
If you looked at a docx resulting from your code, you would see:
<w:tbl></w:tbl>
You are just missing
table.getContent().add(tableRow);
EDIT 24 Sept
You didn't say you until now that you were trying to add your image in a footer!
For this you need to specify that part, so the rel attaches to the footer. So use https://github.com/plutext/docx4j/blob/master/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/BinaryPartAbstractImage.java#L247 or https://github.com/plutext/docx4j/blob/master/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/BinaryPartAbstractImage.java#L339 etc ie one of the signatures which contains Part sourcePart

Replace existng column in MSR

Why does the following MSR code not replace the original column "Var1"?
rxDataStep(inData = input_xdf, outFile = input_xdf, overwrite = TRUE,
transforms = list(Var1 = as.numeric(Var1)),
transformVars = c("Var1")
)
At the moment, RevoScaleR doesn't support changing the type of a variable in an xdf file (even if you write to a different file). The way to do it is to create a new variable, drop the old, and then rename the new variable to the old name.
I would suggest doing this with a transformFunc (see ?rxTransform for more information), so that you can create the new variable and drop the old, all in one step:
rxDataStep(inXdf, outXdf, transformFunc=function(varlst) {
varlst$Var1b <- as.numeric(varlst$Var1)
varlst$Var1 <- NULL
varlst
}, transformVars="Var1")
# this is fast as it only modifies the xdf metadata, not the data itself
names(outXdf)[names(outXdf) == "Var1b"] <- "Var1"
MSR doesn't allow you to overwrite a variable in place with a different variable type.
You have two options: Write to a different variable or write to a different file. I have added a bit of code that shows that both solutions work as stated in MRS 9.0.1. As stated in the comments, there is some point in earlier versions where this might not work. I am not totally sure where that point is, so the code should let you know.
input_xdf <- "test.xdf"
modified_xdf <- "test_out.xdf"
xdf_data <- data.frame(Var1 = as.character(1:10),
Var2 = 2:11,
stringsAsFactors = FALSE)
rxDataStep(inData = xdf_data,
outFile = input_xdf,
rowsPerRead = 5,
overwrite = TRUE)
rxDataStep(inData = input_xdf,
outFile = input_xdf,
overwrite = TRUE,
transforms = list(Var1b = as.numeric(Var1)),
transformVars = c("Var1")
)
rxGetInfo(input_xdf, getVarInfo = TRUE, numRows = 5)
rxDataStep(inData = input_xdf,
outFile = modified_xdf,
transforms = list(Var1 = as.numeric(Var1)),
transformVars = c("Var1")
)
rxGetInfo(modified_xdf, getVarInfo = TRUE, numRows = 5)

Generate a Google Doc (and PDF) of row data when a cell in a row of a sheet is edited

I have a Google Form and Google Sheet set up to collect information I need. I currently have a script that will send me, and the form submitter, an email with a PDF attached that contains the contents of their form submission.
I am trying to edit that script and create another one that sends me a new version of the PDF after I go in and make a change to one of the cells associated with the original form submission (update the status of an issue, add notes, correct grammar, etc.).
This is what I have, I am still very new at programming and would appreciate any help...
(18 NOV #1425) It works! var last_column was not allowing the column after the edited cell to be defined. When I replaced last_column in var data with the actual number of columns that contained the data it worked great! Thank you to everyone who helped me figure this out, and learn a little along the way!
function onSheetEdit(e) {
var source = SpreadsheetApp.getActiveSpreadsheet();
var source_sheet = source.getSheetByName("Form Responses 1");
var range = source_sheet.getDataRange();
var ActiveRow = source_sheet.getActiveRange().getRow();
var data = source_sheet.getRange(ActiveRow,1,1,4).getValues();
var columnA = data[0][0];
var columnB = data[0][1];
var columnC = data[0][2];
var columnD = data[0][3];
(18 NOV #0800) Another friend suggested I change the beginning to this... The email also sends, and now I am getting "1" "1" "/" and "1" in my four document placeholders...
function onSheetEdit(e) {
var source_sheet = e.source.getActiveSheet();
if (source_sheet.getName() !== "Form Responses 1") return; //exit the script if edits are done on other sheets
var data = source_sheet.getRange(e.range.rowStart, 1, 1, source_sheet.getLastColumn())//(StartRow,StartColumn,NumberofRowstoGet,NumberofColumnstoGet)
.getValues()[0];
Logger.log(data);
var columnA = data[0][0];
var columnB = data[0][1];
var columnC = data[0][2];
var columnD = data[0][3];
(17 NOV #1845) I had a friend help me from work and this is as far as we got... The email sends now, but the placeholders are not populating the data correctly in the PDF file attachment. It appears that the only data that is populating is data from row 1, and of that data, only the cell that was edited, and the data in the cells to the right of it...
function onSheetEdit(e) {
var source = SpreadsheetApp.getActiveSpreadsheet();
var source_sheet = source.getSheetByName("Form Responses 1");
var range = source_sheet.getDataRange();
var last_column = source_sheet.getActiveRange().getColumn();
var ActiveRow = source_sheet.getActiveRange().getRow();
var data = source_sheet.getRange(ActiveRow,1,1,last_column).getValues();
var columnA = data[0][0];
var columnB = data[0][1];
var columnC = data[0][2];
var columnD = data[0][3];
(16 NOV #1700) I edited the script to this but still no emails generated. I get this error emailed to me when script fails: "Cell reference out of range (line 13, file "Copy of Form confirmation emails")". Line 13 is "var row".
function onSheetEdit() {
var source = SpreadsheetApp.getActiveSpreadsheet();
var source_sheet = source.getActiveSheet()
var row = source_sheet.getActiveCell().getRow();
var last_column = source_sheet.getLastColumn();
var data = source_sheet.getRange(row,1,1,last_column).getValues();
var columnA = data[0][0];
var columnB = data[0][1];
var columnC = data[0][2];
var columnD = data[0][3];
(16 NOV # 1330) I tried this instead but still no emails generated...
function onSheetEdit() {
var source = SpreadsheetApp.getActiveSpreadsheet();
var source_sheet = source.getActiveSheet()
var row = source_sheet.getActiveCell().getRow();
var last_column = source_sheet.getLastColumn();
var data = source_sheet.getRange(row,1,1,last_column);
var columnA = data.values[0];
var columnB = data.values[1];
var columnC = data.values[2];
var columnD = data.values[3];
Original script...
function onSheetEdit(e) {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var rows = sheet.getActiveCell().getRow();
var columnA = e.values[0];
var columnB = e.values[1];
var columnC = e.values[2];
var columnD = e.values[3];
var docTemplate = "1WyWeCLQQ3en1EbKjOLcWxlOLc0fHHDpZrB9yfXZ7nv8";
var docName = "Test form script";
var carbonCopyEmail = "jeffery.crane#goarmy.com";
var submitterEmail = columnB;
var dataName = columnC;
var submitDate = columnA;
var attachmentName = docName + ' for data ' + dataName
var submitterEmailPlaceholder = 'keyUsername';
var submitDatePlaceholder = 'keyTimestamp';
var templatePlaceholder1 = 'keyQuestion1';
var templatePlaceholder2 = 'keyQuestion2';
var submitterSubject = "Test Script Confirmation Email for data " + dataName;
var submitterBody = "Attached is a PDF confirmation sheet with the details of your submission of data: " + dataName + " submitted on " + submitDate;
var carbonCopySubject = "Test Script Submission Notification Email for data " + dataName;
var carbonCopyBody = "Attached is a PDF confirmation sheet with the details of " + submitterEmail + "'s submission of data: " + dataName + " on " + submitDate;
//Gets document template defined by the docID above, copys it as a new temp doc, and saves the Doc’s id
var copyId = DocsList.getFileById(docTemplate)
.makeCopy(attachmentName)
.getId();
//Open the temporary document
var copyDoc = DocumentApp.openById(copyId);
//Get the document’s body section
var copyBody = copyDoc.getActiveSection();
//POSSIBLE MODIFICATION TO ADD LINES OF CODE
//Replace place holder keys with the spreadsheet values in the google doc template
//This section of the script looks for instances where the key appears in the Google Doc and replaces the instance
//with the defined variable
//For instance, whenever "keyUserName" (defined above as submitterEmailPlaceholder) appears in the Google Doc,
//the value from the spreadsheet in columnB replaces "keyUserName"
copyBody.replaceText(submitDatePlaceholder, columnA);
copyBody.replaceText(submitterEmailPlaceholder, columnB);
copyBody.replaceText(templatePlaceholder1, columnC);
copyBody.replaceText(templatePlaceholder2, columnD);
//Save and close the temporary document
copyDoc.saveAndClose();
//Convert temporary document to PDF
var pdf = DocsList.getFileById(copyId).getAs("application/pdf");
//Attaches the PDF and sends the email to the form submitter
MailApp.sendEmail(submitterEmail, submitterSubject, submitterBody, {htmlBody: submitterBody, attachments: pdf});
//Attaches the PDF and sends the email to the recipients in copyEmail above
MailApp.sendEmail(carbonCopyEmail, carbonCopySubject, carbonCopyBody, {htmlBody: carbonCopyBody, attachments: pdf});
//Deletes the temporary file
DocsList.getFileById(copyId).setTrashed(true);
}
You cannot do data.values, data is a range object, you should do getValues() to get an array of values: data[row][col]
var data = source_sheet.getRange(row,1,1,last_column).getValues()
var columnA = data[0][0];
var columnB = data[1][0];
var columnC = data[2][0];
var columnD = data[3][0];
Why instead of mess manually with the cells values of the responses sheet you don't use the form edit link? You can put it into the mail you are already sending.
You can get it from:
var editLink = lastResponse.getEditResponseUrl();
When the form is submitted again from the edit link the script you wrote script will be run again sending the updated PDF.

Textmate - change #TODO color inside a doc block

I'm able to change an entire documentation block's color inside textmate by using the scope comment.block.documentation
However is it possible to change the color of specific words inside that docblock?
I'm refering to change the color of words or even the entire line like
#TODO
#FIXME
#CHANGED
thanks.
Ok... i was able to find the answer.
Bundles > Bundle Editor > Show Bundle Editor
Select the Language Javascript (note... the language has a L icon inside the Javascript item on the left list)
find for the scope named comment.block.documentation.js and modify it accordingly
{ name = 'comment.block.documentation.js';
begin = '(/\*\*)\s*$';
end = '\*/';
beginCaptures = { 1 = { name = 'punctuation.definition.comment.js'; }; };
endCaptures = { 0 = { name = 'punctuation.definition.comment.js'; }; };
patterns = (
{ name = 'meta.documentation.tag.todo.js';
begin = '((\#)TODO)';
end = '(?=^\s*\*?\s*#|\*/)';
},
{ name = 'meta.documentation.tag.fixme.js';
begin = '((\#)FIXME)';
end = '(?=^\s*\*?\s*#|\*/)';
},
{ name = 'meta.documentation.tag.changed.js';
begin = '((\#)CHANGED)';
end = '(?=^\s*\*?\s*#|\*/)';
},
);
},
after you just have to change the color on the prefs window, using the declared selector!